[英]Retrieving data from a Python Tuple
I am trying to retrieve and reuse data from a JSON object in a for loop in Python. An example of a single JSON object below:我正在尝试在 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 loop example For 循环示例
for i in ids:
print(i["envs"])
print(i["moves"])
envs
will be successfully printed since it is a list. envs
将被成功打印,因为它是一个列表。 However, since moves
is a tuple I receive a KeyError as it is looking for a key in a dictionary.但是,由于moves
是一个元组,我收到一个 KeyError,因为它正在查找字典中的键。 What is the Python recommended way of pulling data from a tuple in this instance. Python 在此实例中从元组中提取数据的推荐方法是什么。 For example, I want to print sequence1
or sequence2
.例如,我想打印sequence1
或sequence2
。
Thanks谢谢
It appears you made a typographic error.看来您输入有误。
From this code从这段代码
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"])
I obtain these results我得到这些结果
['env:remote1', 'env:remote2', 'env:remote3']
{'sequence1': 'half glass full', 'sequence2': 'half glass empty'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.