DEV Community

Cover image for Regex to the Reading History Rescue

Posted on

Regex to the Reading History Rescue

How Many

Continuing to review my reading history

As I am getting back into doing more python I spent a little bit of time to look more specifically at the number of books I have read. While pulling out the authors, and titles, so that I can see how many books I have read and which ones. I read a lot and sometimes I forget which books of a series I read until I start a book then realize, "oh I read this already!"

My local library allows me to download my checkout history and that encompasses EVERYTHING I have checked out. DVD's and books. So I need a way to break those out from the massive text dump that the library gives me.

Many book shelves

As the format is rather standard (as can be seen by my previous post on this: Using Python to slice up my Reading Habits) I decided at this stage I can look up solely by TITLE as its a unique field in each record. As a bonus the Author is included with the record is of a Book, a DVD does not have that on the line.

TITLE        Ponyo / Walt Disney Pictures ; Studio Ghibli, Nippon Television 

TITLE        The boy who would live forever / Frederik Pohl.
Enter fullscreen mode Exit fullscreen mode

Getting back to my Perl roots I made a Regular Expression to simply match on Title.

bookRecord = re.compile(r"(^TITLE.*)", re.MULTILINE)
Enter fullscreen mode Exit fullscreen mode

Simple and easy, to match on every line that starts with TITLE. The Multiline option is there in case titles are long, and I was hoping at some point to create the right regular expression to capture the entire Record. Though its not as important at this stage.

Then after opening the text dump I search through it to match the regular expression and save those lines.

with records as r:
    for match in bookRecord.finditer(history):
        title =
Enter fullscreen mode Exit fullscreen mode

At the end I print out the matching lines, and then check out how many items I have taken home from my local library over the past 6+ years.

for i in bookList:
print("You have checked out " + str(len(bookList)) + " books or videos.")
Enter fullscreen mode Exit fullscreen mode

I'll be playing around with this more to get my Python skills back and start adding more features to make it easy and fun.

Oh, I checked out 717 items.

Top comments (0)