Python and read text file that contains lists with strings and numbers

拥有回忆 提交于 2019-12-01 10:33:04

问题


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!


回答1:


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'))


来源:https://stackoverflow.com/questions/16086955/python-and-read-text-file-that-contains-lists-with-strings-and-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!