0

I am using python 2.7 .I am creating 3 lists (float values (if it matters at all)), i am using json object to save it in a file.

Say for eg.

L1=[1,2,3,4,5]
L2=[11,22,33,44,55]
L3=[22,33,44,55,66]
b={}
b[1]=L1
b[2]=L2
b[3]=L3
json.dump(b,open("file.txt","w"))

I need to read these values back from this "file.txt" into the 3 list. Can anyone please point me to the resource? How do i proceed with retrieving these values?

SeasonalShot
  • 2,357
  • 3
  • 31
  • 49

2 Answers2

2

try

content = json.load(open('file.txt'))

or using a the with context manager to close the file for you:

with open('file.txt') as f:
    content = json.load(f)

Also, read the library's documentation

Community
  • 1
  • 1
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
0

I used this following code:

import json
path=r"file.txt"
for line in open(path):
    obj = json.loads(line)

x=obj['1']
y=obj['2']
z=obj['3']

Now, i will have the List L1 in x, L2 in y and L3 in z

SeasonalShot
  • 2,357
  • 3
  • 31
  • 49