views:

73

answers:

3

Suppose I have an output file that I want to read and each line was created by joining several types together, prepending and appending the list braces,

[('tupleValueA','tupleValueB'), 'someString', ('anotherTupleA','anotherTupleB')]

I want to read the lines in. Now I can read them in, and operate on the string to assign values and types but I was wondering if Python had a higher level method for this.

After building a function to do this I tried to find a higher level approach but didn't find one.

+2  A: 

What you are looking for is eval. But please keep in mind that this function will evaluate and execute the lines. So don't run it on untrusted input ever!

>>> print eval("[('tupleValueA', 1), 'someString']")
[('tupleValueA', 1), 'someString']

If you have control over the script that generate the output file, then I would suggest you use json encoding. JSON format is very similar to the python string representation of lists and dictionaries. And will be much more secure and robust to read from.

>>> import json
>>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
'["foo", {"bar": ["baz", null, 1.0, 2]}]'
>>> json.loads('["foo", {"bar": ["baz", null, 1.0, 2]}]')
["foo", {"bar": ["baz", null, 1.0, 2]}]
Nadia Alramli
Thank you so much, why couldn't I find this.
PyNEwbie
A: 

The problem you describe is conventionally referred to as serialization. JavaScript Object Notation (JSON) is one popular serialization protocol.

las3rjock
A: 

Probably it would be better to store the data with a module like pickle in the first place, instead of using normal strings. This way you avoid a lot of problems that come with eval and get a more robust solution.

sth
Well Pickling will not work. See my question from yesterday. The list is 8 million lines long and when pickled has 379,000 keys. Even when I break the file into three pieces and read them as needed it puts too much of a load. I don't want to go to a database as the keys are unique identifiers and the table columns would be dates (about 3600). But thanks
PyNEwbie