[英]CSV to nested dictionary in Python (without Pandas)
我有一个CSV,我希望通过根据列中的值进行分组将其处理为嵌套字典。 格式如下:
sample, date, depth, analyte, result
'ABC', '01/01/2018', '3', 'LEAD', 0.22
'ABC', '02/01/2018', '3', 'LEAD', 0.25
'ABC', '01/01/2018', '5', 'LEAD', 0.19
'ABC', '02/01/2018', '5', 'LEAD', 0.18
'ABC', '01/01/2018', '3', 'MERCURY', 0.97
'ABC', '02/01/2018', '3', 'MERCURY', 0.95
'ABC', '01/01/2018', '5', 'MERCURY', 0.34
'ABC', '02/01/2018', '5', 'MERCURY', 0.11
'DEF', '01/01/2018', '3', 'LEAD', 0.07
'DEF', '02/01/2018', '3', 'LEAD', 0.04
'DEF', '01/01/2018', '5', 'LEAD', 0.16
'DEF', '02/01/2018', '5', 'LEAD', 0.65
'DEF', '01/01/2018', '3', 'MERCURY', 0.03
'DEF', '02/01/2018', '3', 'MERCURY', 0.01
'DEF', '01/01/2018', '5', 'MERCURY', 0.11
'DEF', '02/01/2018', '5', 'MERCURY', 0.13
我希望我的最终字典看起来像:
dictionary = {sample: {date: {depth: [analyte, result], [analyte, result] ... }}}
我希望我可以通过输入以下内容来遍历字典以访问唯一结果的每个块:
dictionary[sample][date][depth]
例如:
dictionary['ABC']['01/01/2018']['5'] = [['LEAD', 0.19], ['MERCURY', 0.34]]
我想避免使用Pandas,尽管我知道它可能非常适合完成任务-我正在寻找Pythonic解决方案。 这很困难-因为我必须容纳多个样品,多个日期,多个深度和多个分析物。 我是一个初学者,而我尝试过的嵌套循环让我不寒而栗。
任何帮助表示赞赏。
这是使用csv.DictReader
和collections.defaultdict
一种解决方案。 您可以定义特定的嵌套字典结构。 然后遍历输入文件一次,为DictReader
生成的每个字典添加项。
使用类似的方法,您还可以选择带有元组键的字典。 这对于查找将更有效,但会使迭代更加麻烦。
from collections import defaultdict
import csv
d = defaultdict(lambda: defaultdict(lambda: defaultdict(list)))
with open('file.csv', 'r') as fin:
reader = csv.DictReader(fin, quotechar="'", skipinitialspace=True)
for i in reader:
d[i['sample']][i['date']][i['depth']].append([i['analyte'], float(i['result'])])
结果
print(d)
defaultdict({'ABC': defaultdict({'01/01/2018': defaultdict(list,
{'3': [['LEAD', 0.22], ['MERCURY', 0.97]],
'5': [['LEAD', 0.19], ['MERCURY', 0.34]]}),
'02/01/2018': defaultdict(list,
{'3': [['LEAD', 0.25], ['MERCURY', 0.95]],
'5': [['LEAD', 0.18], ['MERCURY', 0.11]]})}),
'DEF': defaultdict({'01/01/2018': defaultdict(list,
{'3': [['LEAD', 0.07],
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.