我有以下 csv 檔案:
Topic,Characteristics,Total
Population and dwellings,Population-2016,183314
Population and dwellings,Population-2011,175779
Population and dwellings,Population percentage change,4.3
Age characteristics,0 to 14 years,30670
Age characteristics,0 to 4 years,9275
Age characteristics,5 to 9 years,10475
我想輸出一個 json 檔案,這樣每個唯一的“主題”都是一個鍵,值是一個字典“特征”:“總計”,即輸出將是:
{
"Population and dwellings": {
"Population-2016": 183314,
"Population-2011": 175779,
"Population percent change": 4.3
},
"Age characteristics": {
"0 to 14 years": 30670,
"0 to 4 years": 9275,
"5 to 9 years": 10475
}
}
我怎樣才能正確地做到這一點?到目前為止,我嘗試過的所有方法都會相互覆寫,我們將不勝感激。謝謝。
uj5u.com熱心網友回復:
您可以使用csv
模塊來讀取檔案并對dict.setdefault
元素進行分組:
import csv
out = {}
with open("your_file.csv", "r") as f_in:
reader = csv.reader(f_in)
next(reader) # skip headers
for topic, characteristics, total in reader:
out.setdefault(topic, {})[characteristics] = float(total)
print(out)
印刷:
{
"Population and dwellings": {
"Population-2016": 183314.0,
"Population-2011": 175779.0,
"Population percentage change": 4.3,
},
"Age characteristics": {
"0 to 14 years": 30670.0,
"0 to 4 years": 9275.0,
"5 to 9 years": 10475.0,
},
}
要從中輸出 JSON,out
您可以執行以下操作:
import json
print(json.dumps(out, indent=4))
uj5u.com熱心網友回復:
我的解決方案與 Andrej 類似,但我使用它defaultdict
來簡化代碼。
import collections
import csv
import json
out = collections.defaultdict(lambda: collections.defaultdict(float))
with open("data.csv") as stream:
next(stream) # Skip the header
reader = csv.reader(stream)
for topic, characteristics, total in reader:
out[topic][characteristics] = float(total)
print(json.dumps(out, indent=4))
輸出:
{
"Population and dwellings": {
"Population-2016": 183314.0,
"Population-2011": 175779.0,
"Population percentage change": 4.3
},
"Age characteristics": {
"0 to 14 years": 30670.0,
"0 to 4 years": 9275.0,
"5 to 9 years": 10475.0
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/534898.html
上一篇:CSV檔案的R讀取函式
下一篇:如何從R中的其他組中減去一個組