diff options
| author | Willem Renes <wrenes@gmail.com> | 2021-06-22 15:03:08 +0200 |
|---|---|---|
| committer | Willem Renes <wrenes@gmail.com> | 2021-06-22 15:03:08 +0200 |
| commit | af39f49a09dc7a1a477a382c507954e153de9ca1 (patch) | |
| tree | 98c9ea54b6e1737507e2bb5c61e8708d588b39c9 /booklist.py | |
| parent | 5d32b516af7993590ffc1a266f06c6a80bcb0b3c (diff) | |
| download | booklist-af39f49a09dc7a1a477a382c507954e153de9ca1.tar.gz booklist-af39f49a09dc7a1a477a382c507954e153de9ca1.zip | |
Diffstat (limited to 'booklist.py')
| -rw-r--r-- | booklist.py | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/booklist.py b/booklist.py index c210a23..3ece6f7 100644 --- a/booklist.py +++ b/booklist.py @@ -13,13 +13,18 @@ Parsing/managing a book wishlist. """ import sys +from pprint import pprint +from collections import defaultdict +def read_booklist(file_name="booklist.txt"): -def read_booklist(file="booklist.txt"): + booklist = [] try: - with open(file) as f: - for line in f: - print(line) + with open(file_name, 'r') as file: + for line in file: + author, title = line.rstrip().split(' - ') + booklist.append([author, title]) + except OSError as exc: tb = sys.exc_info()[-1] lineno = tb.tb_lineno @@ -27,10 +32,21 @@ def read_booklist(file="booklist.txt"): print(f'{exc.strerror} at {filename} line {lineno}.') sys.exit(exc.errno) + return booklist + def main(): """ main function, do stuff """ - read_booklist("booklist.txt") + booklist = read_booklist("booklist.txt") + pprint(booklist) + + newbooklist = defaultdict(list) + + for k, v in booklist: + newbooklist[k].append(v) + + pprint(newbooklist) + if __name__ == '__main__': |
