9

I have a file whose content is in the form of a python list such as the following:

['hello','how','are','you','doing','today','2016','10.004']

Is there any way to read the python file back into a list object? instead of using .read() and having the whole file just read as a string.

EDIT: for those who may be interested i ran into a strange issue using (import ast) as suggested as a solution for the above problem.

the program i used it in has a function which fetches historical stock data from the yahoo finance python module. this function is in no way related or dependent on the function which used ast.literal_eval().

anyways every night after market close i collect new batches of historical data from yahoo finance and last night i ran into an error : simplejson.scanner.jsondecodeerror expecting value.

it was strange because it would collect data just fine for some companies but throw the error for others, and sometime work for the same company but a minute later it would not work. after trying all kinds of things to debug and solve the issue remembered that the import ast was recently added and thought i should try to see if it could have an effect, after removing the import ast the program went back to workin as it normally did.

does anybody know why import ast caused issues? @Apero why did you initially warn against using eval or ast.literal_eval?

martineau
  • 99,260
  • 22
  • 139
  • 249
Mustard Tiger
  • 2,949
  • 6
  • 30
  • 52

2 Answers2

18

You can use ast.literal_eval():

import ast

with open('filename.txt', 'r') as f:
    mylist = ast.literal_eval(f.read())
pp_
  • 3,171
  • 3
  • 17
  • 27
  • Never, ever, ever use ``eval`` or ``ast.literal_eval``. That's the best advice I received when I started coding in python. – DevLounge Mar 24 '16 at 21:09
  • @Apero It's the best solution in this case ... – pp_ Mar 24 '16 at 21:11
  • 1
    @Apero: Why not `ast.literal_eval`? It fits this use case quite well. – Steven Rumbalski Mar 24 '16 at 21:12
  • Agreed, but the real best solution is to rewrite the file IMO ;-) – DevLounge Mar 24 '16 at 21:12
  • @pp_ thank you so much, ast.literal_eval worked. – Mustard Tiger Mar 24 '16 at 21:15
  • @Apero i will also try to do the method of rewriting the file, thanks for the input – Mustard Tiger Mar 24 '16 at 21:16
  • 9
    @Apero: "never, ever" is bad advice. `eval` and `ast.literal_eval` are tools that serve a specific purpose. When you understand how they work and what they do, there is no reason to avoid them under the right circumstances. Instead of saying "never, ever use them", say "gain a deep understanding before using them". – Bryan Oakley Mar 24 '16 at 21:17
  • @BryanOakley I totally agree, but I saw so much dangerous bugs due to some beginners using eval etc that I prefer never using evaluations but solve the real issue. Workarounds are never a good solution. – DevLounge Mar 24 '16 at 21:21
  • question updated after running into issue with import ast – Mustard Tiger Mar 25 '16 at 16:39
8
  1. rename the file from i.e. foo.txt to foo.py
  2. add my_list = in front of that line
  3. in your code: import foo; l = foo.my_list

Simpler, no? ;-)

DevLounge
  • 7,345
  • 2
  • 26
  • 39