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.

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay