0

I have made a list of lists to store score data for a given student. The list is called "class1" and each student has a name followed by 3 scores. I can sort this list fine and can write to a file which show the data in the correct format:

class1=[["Tom",7,2,1],["Jo",8,0,0],["Adelphe",9,0,0]]

When written look like this which I am happy with:

['Tom', 7, 2, 1]
['Jo', 8, 0, 0]
['Adelphe', 9, 0, 0]

The problem comes when I try to read the same list in. Each sublist is read as a string. I can't seem to change this.

Here is the code:

class1 = [line.strip() for line in open("data2.txt", 'r')]

This is what the list looks like when read, as you can see there are apostrophes around each sub list, meaning they are strings within the class1 list:

["['Tom', 7, 2, 1]", "['Jo', 8, 0, 0]", "['Adelphe', 9, 0, 0]"]

Can anyone help me, I've done a lot of searching, but can't find anyone with the same problem.

Thanks, Tom

10
  • You mean split, not strip. Commented Oct 27, 2014 at 11:32
  • 3
    Why not use JSON or CSV instead of inventing your own format? Commented Oct 27, 2014 at 11:33
  • Try using eval(string). If the string represents a list, it should become a python list object. I.e. class1 = [eval(line.strip()) for line in open("data2.txt", 'r')] Commented Oct 27, 2014 at 11:33
  • Why not use namedtuple() ? Commented Oct 27, 2014 at 11:42
  • If the file you're writing to doesn't need to be human readable/editable, you can use the pickle module to write it and load it... will be more efficient that storing as text, then evaluating the input on reading again Commented Oct 27, 2014 at 11:57

1 Answer 1

1
>>> import ast
>>> with open('/path/to/data2.txt', 'r') as classes:
        lines = [ast.literal_eval(line.strip()) for line in classes]

>>> print lines
[['Tom', 7, 2, 1], ['Jo', 8, 0, 0], ['Adelphe', 9, 0, 0]]
Sign up to request clarification or add additional context in comments.

5 Comments

Hi, eval worked. What is the benefit of using: ast.literal_eval? Pardon my ignorance!
@TomSmith ast.literal_eval protects against arbitrary code execution.
@TomSmith eval will evaluate any valid expression which makes it possible for unknown data to harm your computer, like creating a huge string and eating up your processor time and/or memory. Unless you are 100% sure of your data don't use eval, ast.literal_eval is safer because it restricts the types of expressions it will eveluate.
@wwii Note taken! Thanks
@TomSmith: ast.literal_eval() only supports save Python literals as input; strings, numbers, booleans, containers. Nothing more. eval() supports arbitrary Python expressions, including everything that a process can do to damage your computer or turn it into a subservient internet spam bot. It is for that reason I voted to delete the answer you marked as accepted; it is dangerous to use eval(), and you should avoid it for everything but the most specialised of cases where you are sure not to be executing user-supplied input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.