【发布时间】:2015-10-09 06:52:46
【问题描述】:
我必须通过读取文件来创建字典
信息被分成几行
键在括号之间,但并非所有键都是键。只是 [日期] 之后的那些
两个键之间是分成几行的值,但并非所有行都是可选值
最终的结果应该是这样的
d=[键:[单位、高度、场地]]
有些键没有所有的值。然后,如果不存在单位、高度或站点,则该值应使用 '' 或 0
#info in the file
[System]
serial=130204
[Summary]
file_created=2014-11-20 03:02:09
user=j
....#more info
[date]#after this key starts the keys
...
[AX1]
units=m/s
serial_setting=38400
height=70.4
stats=avg
formula=yes
site=site1
[H4]
serial_setting=38100
height=20.6
stats=std
formula=yes
site=site2
[V3]
units=m
...
示例中的最终结果
param={AX1:['m/s',70.4,'site1'],H4:['',20.6,'site2'], V3:['m',0,'']}
我知道如何从列表列表创建字典,但不设置默认值('' 用于字符串值,0 用于数字值)以防某些值丢失
我尝试使用 Collections 中的 defaultdict,但我对这个类还不是很熟悉,可能我没有使用它的所有可能性
感谢您的帮助
【问题讨论】:
标签: python file dictionary collections