1

This is a similar question to many but not quite the same. I have a text file that has about 400,000 lines of text. Each line is essentially a list. For example it looks like [ 'a','b',1,2,'c',3,'d and , e string', 45]

I can read each line of the text file with the following code:

with open('myfile.txt') as f:
    content = f.readlines()

The problem is that each line is read as a string. I would like to get each item of the list. So i thought i would do (for each line):

content[line].split(',')

This almost works, but i run into a problem. Many times in my text file i have a string in the list that has a comma in it (from above i had 'd and , e string'). I don't want this string split up but want it as one item.

If this doesn't make since I want to take the line from my text file [ 'a','b',1,2,'c',3,'d and , e string', 45] and i want 8 separate elements

'a'
'b'
1 
2
'c'
3
'd and , e string'
45

Thanks for the help!

mcfly
  • 1,151
  • 4
  • 33
  • 55
  • 1
    This is a duplicate of lots of `ast.literal_eval` questions, e.g. [this one](http://stackoverflow.com/questions/1894269/convert-string-list-to-list-in-python). – DSM Apr 18 '13 at 15:27
  • I have no idea how i missed that! Thanks for pointing it out -- worked perfectly! – mcfly Apr 18 '13 at 15:46

1 Answers1

4

DSM right

from ast import literal_eval

# use generator 
# instead of allocating 
# 400 000 lists at a time
your_lists = (literal_eval(s) for s in open('filename.txt'))
Community
  • 1
  • 1
indirpir
  • 141
  • 4