Created
November 26, 2016 19:44
-
-
Save umairsd/f55a2a6082800551b37fb842ac8d1e16 to your computer and use it in GitHub Desktop.
Mini-script to parse my read-books file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| with open('books.txt') as f_old, open('newbook.txt', 'w+') as f_new: | |
| for line in f_old: | |
| if "[" in line and "]" in line: | |
| # Process the line only if it has the "[" and "]" characters | |
| book_name, sep1, old_date_str = line.partition("[") | |
| _, _, book_name = book_name.partition("-") | |
| month, _, tmp = old_date_str.partition("/") | |
| day, _, tmp = tmp.partition("/") | |
| year, _, _ = tmp.partition("]") | |
| if len(day) == 1: | |
| day = "0" + day | |
| if len(month) == 1: | |
| month = "0" + month | |
| if len(year) == 2: | |
| year = "20" + year | |
| # String is: "- [YYYY-mm-dd] <Book Name>" | |
| new_line = "- " + "[" + year + "-" + month + "-" + day + "] " + book_name + "\n"; | |
| f_new.write(new_line) | |
| else: | |
| f_new.write(line) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment