[英]Retrieving data from a Python Tuple
我正在尝试在 Python 的 for 循环中从 JSON object 检索和重用数据。下面是单个 JSON object 的示例:
{
"id": "123456789",
"envs": [
"env:remote1",
"env:remote2",
"env:remote3"
],
"moves": {
"sequence1": "half glass full",
"sequence2": "half glass empty"
}
}
For 循环示例
for i in ids:
print(i["envs"])
print(i["moves"])
envs
将被成功打印,因为它是一个列表。 但是,由于moves
是一个元组,我收到一个 KeyError,因为它正在查找字典中的键。 Python 在此实例中从元组中提取数据的推荐方法是什么。 例如,我想打印sequence1
或sequence2
。
谢谢
看来您输入有误。
从这段代码
ids = [
{
"id": "123456789",
"envs": [
"env:remote1",
"env:remote2",
"env:remote3",
],
"moves": {
"sequence1": "half glass full",
"sequence2": "half glass empty",
},
}
]
for i in ids:
print(i["envs"])
print(i["moves"])
我得到这些结果
['env:remote1', 'env:remote2', 'env:remote3']
{'sequence1': 'half glass full', 'sequence2': 'half glass empty'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.