【发布时间】:2017-05-04 07:11:34
【问题描述】:
我有类似的 Python 脚本:
for row in file:
....
....
....
whole_calendar = {}
whole_calendar['ID'] = id
whole_calendar['OPEN'] = open_days
whole_calendar['CLOSED'] = closed_days
whole_calendar['CHECKED'] = checked_days
print(whole_calendar)
生成(迭代)许多相似的字典行:
{'ID': ['133528'], 'OPEN': ['1/1/2016', '2/1/2016', '3/1/2016', '4/1/2016'], 'CLOSED': ['5/1/2016'], 'CHECKED': ['1/7/2016']}
{'ID': ['176987'], 'OPEN': ['3/6/2016', 4/6/2016'], 'CLOSED': [], 'CHECKED': ['1/7/2016',2/7/2016]}
{'ID': ['347697'], 'OPEN': ['1/2/2016'], 'CLOSED': ['1/3/2016'], 'CHECKED': []}
我需要的是把这些字典(行)以这个表的形式写入CSV文件->
133528,'OPEN','1/1/2016'
133528,'OPEN','2/1/2016'
133528,'OPEN','3/1/2016'
133528,'OPEN','4/1/2016'
133528,'CLOSED','5/1/2016'
133528,'CHECKED','1/7/2016'
176987,'OPEN','3/6/2016'
176987,'OPEN','4/6/2016'
176987,'CHECKED','1/7/2016'
176987,'CHECKED','2/7/2016'
347697,'OPEN','1/2/2016'
347697,'CLOSED','1/3/2016'
我只需要使用 Python 2.6 中的内置库(没有 Pandas)
我尝试了一些转换 + 使用 csv.writerow 但我做不到。 你能帮帮我吗?
【问题讨论】:
-
所需的输出可能并不总是按那个顺序排列,因为它是一个如此无序的字典......
-
这不是问题。我将从另一个软件中将其读取为 CSV :-)
标签: python dictionary transformation export-to-csv python-2.6