简体   繁体   English

python中的JSON读取

[英]JSON reading in python

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. 我正在使用python 2.7 。我正在创建3个列表(浮点值(如果有关系的话)),我正在使用json对象将其保存在文件中。

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. 我需要将这些值从此“ file.txt”读回到3列表中。 Can anyone please point me to the resource? 谁能指出我的资源? How do i proceed with retrieving these values? 如何继续获取这些值?

try 尝试

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

or using a the with context manager to close the file for you: 或使用with with上下文管理器为您关闭文件:

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

Also, read the library's documentation 另外,请阅读图书馆的文件

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 现在,我将在x中具有列表L1在y中具有L2, 在z中具有L3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM