1

I have some text stored in a file which looks like this:

[10, 1575311969.9649343, 'Hot Brew', 10, True, 'No tank', 'Organic Red Helles']
[101, 1575311971.3570273, 'Hot Brew', 10, True, 'No tank', 'Organic Red Helles']

Now, I want to get this in my code as a 2d list:

batches = [[10, 1575311969.9649343, 'Hot Brew', 10, True, 'No tank', 'Organic Red Helles'],
           [101, 1575311971.3570273, 'Hot Brew', 10, True, 'No tank', 'Organic Red Helles']]

I tried using the code

from ast import literal_eval

with open('runBatchBackup.txt', 'r') as f:
    batches = literal_eval('[' + f.read() + ']')

But this resulted in

ValueError: malformed node or string: <_ast.Subscript object at 0x03C72BB0>

How can I convert my text file into the format I want?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Sorin Burghiu
  • 715
  • 1
  • 7
  • 26

2 Answers2

0

Your file is newline-delimited but literal_eval needs commas delimiting each sublist for it to be parseable. You can add a call to replace to perform the conversion from newlines to commas:

batches = literal_eval('[' + f.read().replace('\n', ',') + ']')

Result:

[[10, 1575311969.9649343, 'Hot Brew', 10, True, 'No tank', 'Organic Red Helles'], 
[101, 1575311971.3570273, 'Hot Brew', 10, True, 'No tank', 'Organic Red Helles']]

Although the above snippet illustrates the problem clearly, I'd recommend using a list comprehension to apply literal_eval to each line:

batches = [literal_eval(line) for line in f]
ggorlen
  • 44,755
  • 7
  • 76
  • 106
0

I think it makes more sense to literal_eval each line individually and add them to a list in your program:

with open('runBatchBackup.txt', 'r') as f:
    batches = list(map(literal_eval, f))
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96