DEV Community

Cover image for Using Python to slice up my Reading Habits
Michael
Michael

Posted on

Using Python to slice up my Reading Habits

I love my local library. As much as my wife loves to read things on her phone (I give her a pass since she can get more Chinese books online than in our library) I need to have that solid feel of words in my hands.

It's also a great way for me to step away from electronic things and just immerse myself in something or somewhere else.

Since I read a lot I tend to forget now and again on if I have read a specific book, or as I like to read a series, did I read number 3? 7? Where did I leave off as I waited for the next one to come out?

Luckily my library can give me a list of books I have read.

Sadly, that list is a bunch of metadata that I only need 2 lines out of 20+ so taking time to go through it manually is taxing.

Since I wanted to get into Python again I decided to use that to look through a record and get just the Author and Title I need.

Starting with:
Record 717 of 717
LOCATIONS TOWN & CITY
AUTHOR Younger, Heather R., author.
TITLE The art of caring leadership : how leading with heart uplifts
teams and organizations / Heather R. Younger.
EDITION First edition.
PUB INFO Oakland, CA : Berrett-Koehler Publishers, Inc., [2021]
DESCRIPT xvii, 193 pages : illustrations ; 23 cm.
DESCRIPT text txt rdacontent.

I only needed:
AUTHOR Younger, Heather R., author.
TITLE The art of caring leadership : how leading with heart uplifts
teams and organizations / Heather R. Younger.

So at first I just wanted to get through the whole text dump and see what I have.

f = open("export.txt", "r")
print(f.read())
f.close()
Enter fullscreen mode Exit fullscreen mode

But, yikes! I get some weird UTF error! Nothing new here, happens a lot of times when you get text from sources you can't control. So a little modification...

f = open("export.txt", encoding="utf8")
print(f.read())
f.close()
Enter fullscreen mode Exit fullscreen mode

I want to save it, so I can look through just what I want, and maybe do a text search in the result for just what I want. That means ensuring the encoding when writing, as well as adding a regex to get just those two lines. Where I end up with:

import io
import re

with io.open("readingList.txt", "a", encoding="utf8") as f:
    # Get only the two fields we care about
    for line in open("export.txt", encoding="utf8").readlines():
        if re.search("^AUTHOR", line) is not None:
            author = line.replace(", author.", "")
            f.write(author)
        elif re.search("^TITLE", line) is not None:
            title = line
            f.write(title)
f.close()
Enter fullscreen mode Exit fullscreen mode

There you have it, now all I have to do is work on a way to save it so I can automate the searches....or maybe have an input for just the authors I want. It was also a good way to jump back into a language I haven't used in years.

Top comments (0)