擅长:python、mysql、java
<p>你的代码几乎可以正常工作了-首先你应该检查一下。避免使用“list”这样的内置名称来表示变量-其次,顺序有点颠倒。我换了几行,最后说:</p>
<pre><code>file_imported = pd.read_excel('testing.xlsx')
print(file_imported)
list1 = []
for index, row in file_imported.iterrows():
list1.append({
"id" : int(row['id']),
"label" : str(row['label']),
"Customer" : [{'id':str(row['id_customer']),'label':str(row['label_customer'])}]
})
print(list1)
with open('testing.json', 'w') as f:
json.dump(list1, f, indent=True)
</code></pre>
<p>似乎和你想的一样。
顺便说一句,如果您愿意,您也可以使用df.apply来实现这一点</p>