Python: Read file with list as list -
i have placed list in text file. want python read text file , return contents list. however, instead reading content string:
text file:
['a','b','c']
python:
ids=[] writtenfile =open('file.txt') readfile=writtenfile.read() ids= readfile writtenfile.close() print ids
what returned ['a','b','c'] string, not list or text. there way can retrieve ['a','b','c'] text file , store in ids string ids list ['a','b','c'] , not string ? "['a','b','c']"?
use ast.literal_eval
literally evaluate contents of file written though string:
data = ast.literal_eval(open('data.txt').read()) # create file, read contents, evaluate
Comments
Post a Comment