我无法从具有相同名称的多个键中打印值
I'm having trouble printing the values from multiple keys that have the same name
我在打印键“uuid”的值时遇到了问题。 “uuid”键在整个文件中多次出现,而其他只在文件中出现一次的键可以毫无问题地打印出来。所以我想知道是否有可能做我想让它做的事?我收到的错误是 KeyError: 'uuid' 供您参考。
path = os.path.join(startn, listn + endn)
with open(path, encoding='utf-8') as json_file:
auction = json.load(json_file)
print("Type:", type(auction))
print("\nAuction:", auction['uuid'])
文件数据也是这样
{"uuid":"36ff18f6e56d49b18c55cd06df3dfce8","拍卖师":"7c1251d409524cfd96b68da183698676","profile_id":"b7c111408b7c4d57a0665edda28c3b77"}
{“uuid”:“754c3f2a25d949d1907f9c29f761b636”,“拍卖师”:“f281bf681baa4cfea8a798cbe76c15f3”,“profile_id”:“f281bf681baa4cfea8a798cbe76c15f3”}
等...
鉴于您没有给我们完整json而是一个乱七八糟的
{"uuid":"36ff18f6e56d49b18c55cd06df3dfce8","auctioneer":"7c1251d409524cfd96b68da183698676","profile_id":"b7c111408b7c4d57a0665edda28c3b77"} {"uuid":"754c3f2a25d949d1907f9c29f761b636","auctioneer":"f281bf681baa4cfea8a798cbe76c15f3","profile_id":"f281bf681baa4cfea8a798cbe76c15f3"}
我假设实际的json是这样的
[
{
"uuid": "36ff18f6e56d49b18c55cd06df3dfce8",
"auctioneer": "7c1251d409524cfd96b68da183698676",
"profile_id": "b7c111408b7c4d57a0665edda28c3b77"
},
{
"uuid": "754c3f2a25d949d1907f9c29f761b636",
"auctioneer": "f281bf681baa4cfea8a798cbe76c15f3",
"profile_id": "f281bf681baa4cfea8a798cbe76c15f3"
}
]
这是一个json的数组。 python 返回的是一个数组,它不是通过键访问的。你需要像这样迭代数组
with open(path, encoding='utf-8') as json_file:
// Changed the return as plural because this is an array
auctions = json.load(json_file)
// Iterate through your array
for auction in auctions:
print("Type:", type(auction))
print("\nAuction:", auction['uuid'])
我在打印键“uuid”的值时遇到了问题。 “uuid”键在整个文件中多次出现,而其他只在文件中出现一次的键可以毫无问题地打印出来。所以我想知道是否有可能做我想让它做的事?我收到的错误是 KeyError: 'uuid' 供您参考。
path = os.path.join(startn, listn + endn)
with open(path, encoding='utf-8') as json_file:
auction = json.load(json_file)
print("Type:", type(auction))
print("\nAuction:", auction['uuid'])
文件数据也是这样
{"uuid":"36ff18f6e56d49b18c55cd06df3dfce8","拍卖师":"7c1251d409524cfd96b68da183698676","profile_id":"b7c111408b7c4d57a0665edda28c3b77"} {“uuid”:“754c3f2a25d949d1907f9c29f761b636”,“拍卖师”:“f281bf681baa4cfea8a798cbe76c15f3”,“profile_id”:“f281bf681baa4cfea8a798cbe76c15f3”}
等...
鉴于您没有给我们完整json而是一个乱七八糟的
{"uuid":"36ff18f6e56d49b18c55cd06df3dfce8","auctioneer":"7c1251d409524cfd96b68da183698676","profile_id":"b7c111408b7c4d57a0665edda28c3b77"} {"uuid":"754c3f2a25d949d1907f9c29f761b636","auctioneer":"f281bf681baa4cfea8a798cbe76c15f3","profile_id":"f281bf681baa4cfea8a798cbe76c15f3"}
我假设实际的json是这样的
[
{
"uuid": "36ff18f6e56d49b18c55cd06df3dfce8",
"auctioneer": "7c1251d409524cfd96b68da183698676",
"profile_id": "b7c111408b7c4d57a0665edda28c3b77"
},
{
"uuid": "754c3f2a25d949d1907f9c29f761b636",
"auctioneer": "f281bf681baa4cfea8a798cbe76c15f3",
"profile_id": "f281bf681baa4cfea8a798cbe76c15f3"
}
]
这是一个json的数组。 python 返回的是一个数组,它不是通过键访问的。你需要像这样迭代数组
with open(path, encoding='utf-8') as json_file:
// Changed the return as plural because this is an array
auctions = json.load(json_file)
// Iterate through your array
for auction in auctions:
print("Type:", type(auction))
print("\nAuction:", auction['uuid'])