【发布时间】:2023-03-07 13:51:01
【问题描述】:
我在一个 json 文件的 for 循环中从一个变量“单词”打印多个字符串,然后我得到这些名称。
with open('list.json', 'r') as i:
names = json.load(i)
for a in names:
words = (a['id']['name'])
print words
'Vincent'
'Jackson'
'Marie'
'Mold'
'Gary'
'Sameul'
我想将所有这些名称放入一个数组中,并将其保存在变量 'words' 中。
words = ['Vincent','Jackson','Marie','Mold','Gary','Sameul']
【问题讨论】:
-
初始化一个空的
list并在每次迭代中追加a['id']['name']。 -
哇,真快,谢谢!