如何将JSON文件解析为Python列表

问题描述 投票:0回答:4

我试图将一个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
python json python-3.x
4个回答
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循环只需要改变一点。

PS你在阅读文件时不需要指定'r'。

您还可以获得这样的单个值:

>>> 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

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)

对不起我的英语不好 :)


0
投票

data["Bid"]是一个字典,并通过迭代dict:

for x in data["Bid"]:

你实际上正在迭代dict的键,这些键是字符串,不能用像x["0"]这样的非整数索引。

如果你想在0 dict的data["Bid"]键下迭代子列表,你应该做:

for x in data["Bid"]['0']:
    print(x)

0
投票

快速回答(TL; DR)

  • 之前转换 for x in data["Bid"]: print(x["0"])
  • 进入后 for x in data["Bid"]: print(data["Bid"][x])

问题

  • 对加载的JSON数据的Python数据引用未产生所需的结果

  • 将JSON地址正确地标识为字典键或字符串
  • 在原始示例中,print(x["0"])是一个不正确的引用,因为x是一个字符串(字典键)
  • 这可以通过使用python 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]]
© www.soinside.com 2019 - 2024. All rights reserved.