Python and read text file that contains lists with strings and numbers -
this similar question many not quite same. have text file has 400,000 lines of text. each line list. example looks [ 'a','b',1,2,'c',3,'d , , e string', 45]
i can read each line of text file following code:
with open('myfile.txt') f: content = f.readlines()
the problem each line read string. each item of list. thought (for each line):
content[line].split(',')
this works, run problem. many times in text file have string in list has comma in (from above had 'd , , e string'). don't want string split want 1 item.
if doesn't make since want take line text file [ 'a','b',1,2,'c',3,'d , , e string', 45]
, want 8 separate elements
'a' 'b' 1 2 'c' 3 'd , , e string' 45
thanks help!
dsm right
from ast import literal_eval # use generator # instead of allocating # 400 000 lists @ time your_lists = (literal_eval(s) s in open('filename.txt'))
Comments
Post a Comment