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

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -