码迷,mamicode.com
首页 > 编程语言 > 详细

Python解析CSV中的多维字典

时间:2019-03-30 18:54:08      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:ade   inf   索引   根据   with   结构   with open   bsp   key   

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]

 

Python解析CSV中的多维字典

标签:ade   inf   索引   根据   with   结构   with open   bsp   key   

原文地址:https://www.cnblogs.com/dimdusk/p/10628327.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!