當我將 json 檔案轉換為 csv 時出現問題,我將 csv 檔案轉換為 json 并且它可以作業,但是當我將 json 檔案轉換為 csv 檔案時它不起作用!
這是我的 Python 代碼
with open('orders.json') as json_file:
data = json.load(json_file)
order_data = data['orders']
# now we will open a file for writing
data_file = open('data_file.csv', 'w')
# create the csv writer object
csv_writer = csv.writer(data_file)
# Counter variable used for writing
# headers to the CSV file
count = 0
for ord in order_data:
if count == 0:
# Writing headers of CSV file
header = ord.keys()
csv_writer.writerow(header)
count = 1
# Writing data of CSV file
csv_writer.writerow(ord.values())
data_file.close()
輸出是這樣的

csv檔案上有空行如何處理這個問題?
這里是json檔案

uj5u.com熱心網友回復:
在 Python 3 中,所需的語法發生了變化,并且 csv 模塊現在可以使用 text mode 'w',但還需要newline=''(empty string) 引數來抑制 Windows 行翻譯。
with open('/pythonwork/thefile_subset11.csv', 'w', newline='') as outfile:
writer = csv.writer(outfile)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/460587.html
標籤:Python json python-3.x CSV
