Skip to content
Advertisement

Output of split is not what I was expecting

I’m just learning python, and I’m having some problems reading a .txt file that I created. My objective: I have a txt file with a list of strings. I’m trying to read, process it and save every letter into a new list.


example2.txt file: [one, two, THREE, one, two, ten, eight,cat, dog, bird, fish] [Alonso, Alicia, Bob, Lynn] , [red, blue, green, pink, cyan]


My output [‘one, two, THREE, one, two, ten, eight, cat, dog, bird, fish]n’] [‘Alonso, Alicia, Bob, Lynn], [red, blue, green, pink, cyan’]

What I was expecting was something like this: ['one','two','THREE','one','two','ten','eight','cat','dog','bird','fish','Alonso','Alicia','Bob','Lynn','red','blue','green','pink','cyan']

My code in python This is what I tried; you can ignore the comments

JavaScript

I also was reading the documentation on how to use re, but I don’t know if it is just me or is hard to understand.

I tried experimenting with what I read, but I’m still not getting what I wanted.

I even try this:

JavaScript

Output

JavaScript

As you can see, it kind of works. However, between Lynn and red, the braces and the comma do not disappear somehow.

Thank you for the time and help

Advertisement

Answer

You might just find that doing an re.findall on the pattern w+ works here:

JavaScript

This prints:

JavaScript
Advertisement