-2

Hi I have big text file and I want to read the file in python and save the data on list. the structure of the file is like this

[{"address":"office1","id":"3311"},{"address":"office2","id":"3322"}]
[{"address":"office3","id":"3312"},{"address":"office4","id":"3323"}]

I want to save the first line in one list and the second line in different list. Can you please explain how to do it.

user2521791
  • 1,621
  • 2
  • 14
  • 13

1 Answers1

1

file.txt

[{"address":"office1","id":"3311"},{"address":"office2","id":"3322"}]
[{"address":"office3","id":"3312"},{"address":"office4","id":"3323"}]

code:

import ast
lists = []
for line in open('file.txt'):
    lists.append(ast.literal_eval(line.strip()))

>>> lists
[[{'id': '3311', 'address': 'office1'}, {'id': '3322', 'address': 'office2'}], [{'id': '3312', 'address': 'office3'}, {'id': '3323', 'address': 'office4'}]]
Elisha
  • 4,811
  • 4
  • 30
  • 46