Skip to content

Instantly share code, notes, and snippets.

@umairsd
Created November 26, 2016 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save umairsd/f55a2a6082800551b37fb842ac8d1e16 to your computer and use it in GitHub Desktop.
Save umairsd/f55a2a6082800551b37fb842ac8d1e16 to your computer and use it in GitHub Desktop.
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