繁体   English   中英

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

[英]How to parse JSON file into Python list

我正在尝试将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

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

快速解答(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]]

data["Bid"]是一个字典,并通过以下操作迭代一个字典:

for x in data["Bid"]:

您实际上是在遍历dict的键,这些键是字符串,不能用x["0"]这样的非整数索引。

如果您打算遍历data["Bid"] dict的0键下的子列表,则应该执行以下操作:

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

您的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)

对不起我的英语不好 :)

暂无
暂无

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

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