【发布时间】:2021-10-20 09:37:52
【问题描述】:
我正在尝试迭代 JSON 文件及其元素。问题是,如果第一次迭代(元素)的值是 bool/float/int 或 NoneType 类型,则会引发错误消息。
这是我的尝试(它适用于 bool、float 和 int 但会抛出 TypeError: 'NoneType' object is not iterable):
for element in json:
if type(element) != bool \
and type(element) != float \
and type(element) != int \
and type(element) is not None:
for value in json[element]:
print(value)
还有没有办法缩短 Python 中的代码?
【问题讨论】:
-
第二个 for 循环的缩进错误。
标签: python loops if-statement iteration