Search code examples
pythonjsonlistfile-iodump

Python - How to read lines of lists in a text file directly into lists in python


I have received a text file generated by a json dump in python that looks like this:

[0.1,0.1,0.2,0.3]
[0.1,0.3,0.4,0.3]
[0.1,0.1,0.3,0.3]
[0.3,0.1,0.5,0.3]
.
.
.
[0.1,0.1,0.3,0.3]
[0.3,0.4,0.6,0.3]

and so on for a considerable amount of lines ~>10,000,000

I would like to figure out the quickest/most efficient way to read from the file and actually convert them into lists.

I have a program that has a for loop that runs a particular operation with lists:

for x in range(filelength):
    for y in list(each line from the file):
        use the numbers from each list to perform certain operations

I was thinking of parsing out all the brackets from the text file and feeding each value comma separated into a blank list for each line (which would probably be slow and time consuming), but I thought there might be a feature of python to convert a list represented as a string easily into an actual list in python quickly.

Any thoughts or suggestions would be appreciated.


Solution

  • Use ast.literal_eval() to parse each line back into a Python list:

    import ast
    
    with open(filename, 'r') as fh:
        for line in fh:
            listobj = ast.literal_eval(line)
    

    ast.literal_eval() takes a string and interprets it as Python literal values; lists and floating point values are directly supported:

    >>> ast.literal_eval('[0.1,0.1,0.2,0.3]\n')
    [0.1, 0.1, 0.2, 0.3]