【发布时间】:2018-02-10 03:30:33
【问题描述】:
我正在尝试迭代 json 数据。这是我拥有的数据结构
import requests
import re
url = "https://web.archive.org/__wb/calendarcaptures?url=http%3A%2F%2Fwww.unibocconi.it&selected_year=2014"
# You can see the data structure by copy-pasting the link
data = requests.get(url).json()
for x in data:
for y in x:
for z in y:
for xx in z:
start1 = "'ts': "
start2 = "'st': "
h = str(xx)
a = re.search('%s(.*)' % (start1) , h).group(1)
date = a[:16].replace("[", "").replace("]", "")
date = re.sub("[^0-9]", "", date)
b = re.search('%s(.*)' % (start2) , h).group(1)
status = b[:5].replace("[", "").replace("]", "")
我知道,我不能遍历 None 类型的对象。但我无法解决几个小时的问题。有任何想法吗? 注意:我是通过 requests 直接从网上获取 json 数据
【问题讨论】:
-
@pault 似乎是从他的代码的第一行开始定义的,开头是
data = -
None在 JSON 中无效。应该是null。 -
其实这是我得到的,但 None 可能是一个字符串...
-
@edyvedy13 可能是?也许你应该检查并确定。不管怎样,你真正想用这些数据做什么?
-
您不能对字符串进行嵌套循环。
x正在迭代字符,所以没有什么可以迭代了。
标签: python json iteration nonetype