[Tutor] regex help for a noob

Thomas A. Anderson thomas.anderson at little-beak.com
Mon Feb 15 15:39:50 EST 2021


Hello,

I have a long file that has single characters I would like to be
extracted, and added to a list in the end.

The file has many lines, not all have the text I am looking for.

The single characters I am looking for are nestled within a ("_"), i.e.
parenthesis and double quote.

I have tried the following code:


import re

def getlist():
    """ creates a list from file """ list = []
    dataload = open("/Users/drexl/Lyntin/sample.txt", "r")
    regExp = '\".*?\"' for line in dataload.readlines():
        x = re.findall(regExp, line)
        if x:
            list.append(x)

    print list


getlist()

I get the desired result, more or less, slightly more on the less side =(

I am getting this as a list output:
[['"n"'], ['"n"'], ['"e"'], ['"w"'], ['"n"']]

where I would like a more basic list:
list = ['n', 'n', 'e', 'w', 'n']

I have tried various other regex expressions, but they only give me worse or the same results.
So, I don't think it is regex related? But somewhere else, I am missing something?

Thanks for the help in advance.



More information about the Tutor mailing list