Python: How to use %%% when parsing text
Question: I'm trying to parse the text in the ebooks at gutenberg.org to extract info
about the books, for example, the title.

Every book on there has a line like this:

    
    
    *** START OF THIS PROJECT GUTENBERG EBOOK THE ADVENTURES OF SHERLOCK HOLMES *** 
    

I'd like to use some thing like this:

    
    
    book_name=()
    index = 0
    for line in finalLines:
        index+=1
        if  "*** START OF THIS PROJECT GUTENBERG EBOOK "%%%"***" in line:
            print(index, line)
            book_name=%%%
    

but I'm obviously not doing it right. Can someone show me how it's done??


Answer: Regex is the way to go:

    
    
    import re
    
    title_regex = re.compile(r'\*{3} START OF THIS PROJECT GUTENBERG EBOOK (.*?) \*{3}')
    
    for index, line in enumerate(finalLines):
        match = title_regex.match(line)
    
        if match:
            book_name = match.group(1)
            print(index, book_name)
    

You can also parse it line-by-line:

    
    
    import urllib.request
    
    url = 'http://www.gutenberg.org/cache/epub/1342/pg1342.txt'
    book = urllib.request.urlopen(url)
    lines = book.readlines()
    book.close()
    
    reached_start = False
    metadata = {}
    
    for index, line in enumerate(lines):
        if line.startswith('***'):
            if not reached_start:
                reached_start = True
            else:
                break
    
        if not reached_start and ':' in line:
            key, _, value = line.partition(':')
            metadata[key.lower()] = value
    

