[Tutor] regex help for a noob

Alex Kleider alexkleider at gmail.com
Mon Feb 15 19:07:38 EST 2021


Others have already provided solutions but no one has mentioned what for me
is the best "goto" for python regex:
https://docs.python.org/3/howto/regex.html
It not only explains groups but also how to name them (a Python provided
extra feature, I believe) which might be useful for you.


On Mon, Feb 15, 2021 at 2:09 PM Thomas A. Anderson via Tutor <
tutor at python.org> wrote:

> 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.
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list