[英]How can I convert a CSV file, which is nested by means of columns, to a nested dictionary with Python?
我有一個類別的谷歌表。
[Google Sheets of nested Categories][1] [1]: https://i.stack.imgur.com/3OAi5.png / 我將其導出到 csv 文件中,結果如下:
Substructure,,,
,Foundations,,
,,Standard Foundations,
,,,Wall Foundations
,,,Column Foundations
,,,Standard Foundation Supplementary Components
,,Special Foundations,
,,,Driven Piles
,,,Bored Piles
,,,Caissons
,,,Special Foundation Walls
,,,Foundation Anchors
,,,Underpinning
,,,Raft Foundations
,,,Pile Caps
,,,Grade Beams
使用 Python,我想將此 CSV 文件轉換為具有以下格式的嵌套字典:
categories = [
{
id: 0,
title: 'parent'
}, {
id: 1,
title: 'parent',
subs: [
{
id: 10,
title: 'child'
}, {
id: 11,
title: 'child'
}, {
id: 12,
title: 'child'
}
]
}, {
id: 2,
title: 'parent'
},
// more data here
];
因此,需要明確的是,每個 csv 行都應該像這樣添加到字典中:{id:x,title:y},如果它有孩子,它應該看起來像這樣:{id:x,title:y,subs :[逗號分隔的兒童字典]}。
我已經在這里使用了類似的問題花了大約一天半的時間,但是對於我目前的技能水平來說,它們都太不同了,無法讓它們為此工作。 我感覺很糟糕,非常感謝一些幫助。 如果可能的話,我也想在其他場景中使用該解決方案,包括不同級別的孩子。 這個例子為孩子設置了三個級別,有些只有兩個或一個。
我真的很感謝你的幫助。
遞歸!
import csv
from pprint import pprint
filename = 'myfile.csv'
with open(filename) as f:
matrix = list(csv.reader(f))
current_id = -1
def next_id():
global current_id
current_id += 1
return current_id
def group(column, rows):
if column == len(matrix[0]) - 1:
return [
{'id': next_id(), 'title': row[column].strip()}
for row in rows
]
result = []
item = None
sub = None
for row in rows:
title = row[column]
if title:
if item:
item['subs'] = group(column + 1, sub)
item = {'id': next_id(), 'title': title.strip()}
result.append(item)
sub = []
else:
sub.append(row)
item['subs'] = group(column + 1, sub)
return result
pprint(group(0, matrix))
Output:
[{'id': 0,
'subs': [{'id': 1,
'subs': [{'id': 2,
'subs': [{'id': 3, 'title': 'Wall Foundations'},
{'id': 4, 'title': 'Column Foundations'},
{'id': 5,
'title': 'Standard Foundation Supplementary Components'}],
'title': 'Standard Foundations'},
{'id': 6,
'subs': [{'id': 7, 'title': 'Driven Piles'},
{'id': 8, 'title': 'Bored Piles'},
{'id': 9, 'title': 'Caissons'},
{'id': 10,
'title': 'Special Foundation Walls'},
{'id': 11, 'title': 'Foundation Anchors'},
{'id': 12, 'title': 'Underpinning'},
{'id': 13, 'title': 'Raft Foundations'},
{'id': 14, 'title': 'Pile Caps'},
{'id': 15, 'title': 'Grade Beams'}],
'title': 'Special Foundations'}],
'title': 'Foundations'}],
'title': 'Substructure'}]
我相信您正在尋找的語法如下:
with open('file.csv', mode='r') as infile:
reader = csv.reader(infile)
with open('file_new.csv', mode='w') as outfile:
writer = csv.writer(outfile)
mydict = {rows[0]:rows[1] for rows in reader}
或者,對於 python <= 2.7.1,您需要:
mydict = dict((rows[0],rows[1]) for rows in reader)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.