1 回答

TA貢獻1828條經驗 獲得超6個贊
僅供參考,字典鍵在 python 中必須是唯一的。所以你期望的輸出是不可能的。
建議:您可以通過以下方式存儲數據:
car = {
"Honda": [
{
"model": "Civic",
"variant": "TC VTEC Premium",
"year": "2020",
"engineCapacity": "1498cc",
"transmission": "Automatic",
"seatCapacity": "5"
},
{
"model": "Accord",
"variant": "TC",
"year": "2020",
"engineCapacity": "1498 cc",
"transmission": "Automatic",
"seatCapacity": "5"
}
],
"BMW": [
{
"model": "XYZ",
"year": "2020",
"transmission": "Automatic",
"seatCapacity": "5"
},
{
"model": "ABC",
"year": "2020",
"engineCapacity": "1498 cc",
"transmission": "Automatic",
"seatCapacity": "5"
}
]
}
您可以使用下面的部分代碼從文件中逐行讀取數據,您可以編寫自己的代碼以上述格式存儲數據
import json
with open('PATH_TO_FILE/FILE_NAME.json') as f:
data = f
for line in f:
line = json.loads(line)
# YOUR CODE HERE
添加回答
舉報