TAGS :Viewed: 1 - Published at: a few seconds ago

[ Read a list out of a txt element by element ]

I have a txt file with about 5000 names("MARY","PATRICIA","LINDA",...). I want to put that names into a list in python. I searched for a solution, but I could only find some methodes to read txt files line by line or as one element, but I need to read it element by element. Is there a nice way to do that?

Answer 1

Python itself can't do that out of the box, as in you can't say something like text.readlines() and have it work on each separate element. What you can do is read the text and use the split method of strings. For exsmple, if the data is separated by commas, text.split(",") will return you a list that contains all the elements.

If words are separated by spaces only, you can just call the split() method without any arguments.