Python解析CSV中的多维字典

CSV文件结构如下,其中字段A为唯一

代码如下,Python27

 1 with open(file_obj+'TEST.CSV','r') as f:
 2     #转为字典
 3      Reader=csv.DictReader(f)
 4     #转为列表,列表中多维字典
 5      csvlist1 = list(Reader)
 6     #将A当做为索引
 7      csvlist2 = [row['A'] for row in csvlist1]
 8     #根据A的值找到下标值
 9      n = csvlist2.index(A VALUE)
10     #根据下标值获取对应的字典
11      csvlist3 = csvlist1[n]
12     #根据字典的KEY获取Value
13      BValue = csvlist3['B']

 

posted @ 2019-03-30 17:59  Dimdusk  阅读(788)  评论(0编辑  收藏  举报