Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mini-script to parse my read-books file
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
You can’t perform that action at this time.