使用pythonv3.7.8,我正在嘗試從JSON檔案中讀取資料并將一些資料寫入CSV. JSON包含希臘語和拉丁語字符。
來自的資料JSON被正確讀取(我列印它們)。但是,當我將資料寫入CSV, 希臘字符時,并沒有正確顯示。
這是我的代碼:
import json
import csv
# Opening JSON file to read data
f = open('test_2021-11-22-Andrias_lecture.json', 'r',encoding= 'utf-8')
# returns JSON object as a dictionary
data = json.load(f)
namesRowList=[]
namesColumnList=[]
connectionCountList=[]
connectionCountListTemp=[]
connectionsRow = {'':''}
# Iterating through the json list
for i in data['playerArray']:
namesRowList.append(i['score'])
namesColumnList.append(i['score'])
connectionsRowTemp = {i['score']:''}
connectionsRow.update(connectionsRowTemp)
print(connectionsRow)
# Open CSV file to store data
with open('matrix_10_Jan_2022.csv', 'w', newline='', encoding='utf-8') as file:
headerList = [''] namesRowList.copy()
dw = csv.DictWriter(file, delimiter=';', fieldnames=headerList)
dw.writeheader()
for i in data['playerArray']:
name = i['score']
connections = i['connections']
connectionsRow['']=name
index = 0
for name in namesRowList:
for con in connections:
if (name == con):
connectionsRow[name] = 1
else:
connectionsRow[name] = 0
index = index 1
dw.writerow(connectionsRow)
file.close()
uj5u.com熱心網友回復:
Excel 需要位元組順序標記 (BOM) 簽名,否則它將以本地 ANSI 編碼解釋檔案。有一個編解碼器,utf-8-sig.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/410007.html
標籤:
上一篇:比較兩個檔案并找出差異
