我正在尝试将JSON文件解析为Python,但无法打印我试图从JSON中提取的任何特定数据。
如何将这些JSON数据放在单独的数组中,以便可以在Python中使用它们?
这是JSON文件:
{
"Ask":
{"0":[[9.13,30200],[9.14,106946],[9.15,53072],[9.16,58104],[9.17,45589]],
"1":[[9.14,106946],[9.15,53072],[9.16,58104],[9.17,45589],[9.18,37521]] },
"Bid":
{"0":[[9.12,198807],[9.11,1110],[9.1,42110],[9.09,84381],[9.08,98178]],
"1":[[9.13,13500],[9.12,198807],[9.11,1110],[9.1,42110],[9.09,84381]]}
}
这是我的代码:
import json
with open("JSON Directory") as BOB:
data=json.load(BOB)
for x in data["Bid"]:
print(x["0"])
我收到此错误:
TypeError: string indices must be integers
答案 0 :(得分:1)
for循环只有一个小问题。
james@VIII:~/Desktop$ ls
f.txt
james@VIII:~/Desktop$ cat f.txt
{
"Ask":
{"0":[[9.13,30200],[9.14,106946],[9.15,53072],[9.16,58104],[9.17,45589]],
"1":[[9.14,106946],[9.15,53072],[9.16,58104],[9.17,45589],[9.18,37521]] },
"Bid":
{"0":[[9.12,198807],[9.11,1110],[9.1,42110],[9.09,84381],[9.08,98178]],
"1":[[9.13,13500],[9.12,198807],[9.11,1110],[9.1,42110],[9.09,84381]]}
}
james@VIII:~/Desktop$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> with open('f.txt') as f_in:
... data = json.load(f_in)
...
>>> data
{'Ask': {'0': [[9.13, 30200], [9.14, 106946], [9.15, 53072], [9.16, 58104], [9.17, 45589]], '1': [[9.14, 106946], [9.15, 53072], [9.16, 58104], [9.17, 45589], [9.18, 37521]]}, 'Bid': {'0': [[9.12, 198807], [9.11, 1110], [9.1, 42110], [9.09, 84381], [9.08, 98178]], '1': [[9.13, 13500], [9.12, 198807], [9.11, 1110], [9.1, 42110], [9.09, 84381]]}}
>>> data['Ask']
{'0': [[9.13, 30200], [9.14, 106946], [9.15, 53072], [9.16, 58104], [9.17, 45589]], '1': [[9.14, 106946], [9.15, 53072], [9.16, 58104], [9.17, 45589], [9.18, 37521]]}
>>>
>>> data['Bid']
{'0': [[9.12, 198807], [9.11, 1110], [9.1, 42110], [9.09, 84381], [9.08, 98178]], '1': [[9.13, 13500], [9.12, 198807], [9.11, 1110], [9.1, 42110], [9.09, 84381]]}
>>> for x in data['Bid']['0']:
... print(x)
...
[9.12, 198807]
[9.11, 1110]
[9.1, 42110]
[9.09, 84381]
[9.08, 98178]
您的for循环只需稍作更改。
在读取文件时不需要指定'r'的PS。
您还可以像这样获得单个值:
>>> for x in data['Bid']['0']:
... print(str(x[0]) + ': ' + str(x[1]))
...
9.12: 198807
9.11: 1110
9.1: 42110
9.09: 84381
9.08: 98178
答案 1 :(得分:0)
转换之前
for x in data["Bid"]:
print(x["0"])
进入之后
for x in data["Bid"]:
print(data["Bid"][x])
print(x["0"])
是不正确的引用,因为x
是字符串(字典键)pprint.pprint()
对您要查找的内容进行故障排除来验证## import pprint
## we use this to inspect the data to make sure we are
## working with what we expect
import pprint
pass
# [...] (loading code omitted)
for x in data["Bid"]:
pprint.pprint(x)
# u'1'
# u'0'
## pprint the values
for x in data.values():
pprint.pprint(x)
# {u'0': [[9.13, 30200],
# [9.14, 106946],
# [9.15, 53072],
# [9.16, 58104],
# [9.17, 45589]],
# u'1': [[9.14, 106946],
# [9.15, 53072],
# [9.16, 58104],
# [9.17, 45589],
# [9.18, 37521]]}
# {u'0': [[9.12, 198807],
# [9.11, 1110],
# [9.1, 42110],
# [9.09, 84381],
# [9.08, 98178]],
# u'1': [[9.13, 13500],
# [9.12, 198807],
# [9.11, 1110],
# [9.1, 42110],
# [9.09, 84381]]}
## we are looking at dictionary keys with `x`
## if we want to iterate the dictionary keys in data["Bid"]
## we need to correctly reference what we are looking for
for x in data["Bid"]:
print(data["Bid"][x])
# [[9.13, 13500], [9.12, 198807], [9.11, 1110], [9.1, 42110], [9.09, 84381]]
# [[9.12, 198807], [9.11, 1110], [9.1, 42110], [9.09, 84381], [9.08, 98178]]
答案 2 :(得分:0)
值data["Bid"]
是一个字典,并通过以下操作迭代一个字典:
for x in data["Bid"]:
您实际上是在遍历dict的键,这些键是字符串,并且无法使用x["0"]
之类的非整数进行索引。
如果您打算遍历0
字典的data["Bid"]
键下的子列表,则应该执行以下操作:
for x in data["Bid"]['0']:
print(x)
答案 3 :(得分:0)
您的for循环是dict键。
for x in data["Bid"]:
print(type(x))
# <class 'str'>
尝试:
for x in data['Bid']['0']:
print(x)
或
for x in data['Bid'].values():
print(x)
对不起,我的英语:)