[英]how to read a json array from specific line of a file with python without using eval function
i have a file which has that json array我有一个包含 json 数组的文件
[
{'sector':'1','info':[{'id':'1234','pos':'55039974','risk':'low'},{'id':'3333','pos':'44530354','risk':'middle'}]},
{'sector':'2','info':[{'id':'2434','pos':'45455554','risk':'high'},{'id':'4444','pos':'4454555','risk':'high'}]}
]
as a single line作为单行
[{'sector':'1','info':[{'id':'1234','pos':'55039974','risk':'low'},{'id':'3333','pos':'44530354','risk':'middle'}]},{'sector':'2','info':[{'id':'2434','pos':'45455554','risk':'high'},{'id':'4444','pos':'4454555','risk':'high'}]}]
in a file as line 2. how can i read just line 2 which is that json array out of that file and print it with在文件中作为第 2 行。我怎样才能从该文件中读取第 2 行,即 json 数组并用
print(str(lines[0]['sector'][0]['id']))
? ? i dont want to use eval function, because if i use a huge array like 1E6 entries eval increase my RAM up to 6Gb.
我不想使用 eval function,因为如果我使用像 1E6 条目这样的巨大数组,eval 会将我的 RAM 增加到 6Gb。 Thats the reason why i try to figure out how it would works with other functions to read a string and convert it to an array.
这就是我试图弄清楚它如何与其他函数一起读取字符串并将其转换为数组的原因。 thx for any help
谢谢任何帮助
Update : i tried:更新:我试过:
with open('file.txt') as f:
"""
v1:
t = f.readlines()[2]
s = json.loads(t)
print(s[0]['sector'])
v1 output:
Traceback (most recent call last): s = json.loads(t)
File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
...
...
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
v2:
t = f.readlines()[2]
x = json.dumps(t, indent=4)
#x = json.dumps(t)
s = json.loads(x)
print(s[0]['sector'])
v2 output:
TypeError: string indices must be integers
v3:
t = f.readlines()[2]
x = json.dumps(t, indent=4)
s = json.JSONDecoder().decode(json.JSONEncoder().encode(x))
#s = json.JSONDecoder().decode(x)
#s = json.JSONEncoder().encode(x)
print(s[0]['sector'])
v3 output:
TypeError: string indices must be integers
"""
Update 2 The array is the problem!更新 2数组是问题! this constalation without [ and sector 2 works
没有 [ 和第 2 区的这个星座有效
{"sector":"1","info":[{"id":"1234","pos":"55039974","risk":"low"},{"id":"RS11591147x","pos":"44530354","risk":"middle"}]}
Update 3 - (closed) the array is ok, but i had to replace the ' to " in each array. and the memory just increase from 450Mb up to 700Mb. thats a huge to 6Gb. the problem is that the txt files increase vom 30Mb to 50Mb but who cares? xD更新 3 - (关闭)阵列没问题,但我不得不将每个阵列中的 ' 替换为 "。memory 只是从 450Mb 增加到 700Mb。这是一个巨大的 6Gb。问题是 txt 文件增加了 vom 30Mb 到 50Mb 但谁在乎呢?xD
whereIsMyArray = 1
with open('file.txt', 'r') as fp:
line = fp.readlines()[whereIsMyArray].replace("'", '"')
data = json.loads(line)
print(data[1]['info'][1]["risk"])
You could try with json library function json.loads
:您可以尝试使用 json 库 function
json.loads
:
import json
with open('yourfile.txt') as f:
s=f.readlines()[1]
result=json.loads(s)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.