如何将包含列表的字典转换为特定行?
How to transform dictionaries containing lists to specific rows?
我有 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': []}
我需要的是以table->
的形式将这些字典(行)写入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 但我做不到。
你能帮我吗?
您正在使用中间字典步骤使事情复杂化。您可以在从旧文件中读取行时写入新文件。
import csv
with open('new_format.csv', 'w') as csvfile:
fieldnames = ['id', 'status', 'date']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
for row in file:
....
....
....
[writer.writerow({'id': id, 'status': 'OPEN', 'date': d}) for d in open_days]
[writer.writerow({'id': id, 'status': 'CLOSED', 'date': d}) for d in closed_days]
[writer.writerow({'id': id, 'status': 'CHECKED', 'date': d}) for d in checked_days]
Andrew 的回答并不是 100% 适合我,但在我做了一些小改动之后:
import csv
with open('new_format.csv', 'w') as csvfile:
fieldnames = ['id', 'status', 'date']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
for row in file:
....
....
....
writer.writerows({'id': id, 'status': 'OPEN', 'date': d} for d in open_days)
writer.writerows({'id': id, 'status': 'CLOSED', 'date': d} for d in closed_days)
writer.writerows({'id': id, 'status': 'CHECKED', 'date': d} for d in checked_days)
确实如此,而且看起来几乎正确(我不知道为什么我得到空白行):
...
8: 4882410,CLOSED,2016-11-09
9:
10: 4882410,CLOSED,2016-11-10
11:
12: 4882410,CLOSED,2016-11-11
...
我有 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': []}
我需要的是以table->
的形式将这些字典(行)写入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 但我做不到。 你能帮我吗?
您正在使用中间字典步骤使事情复杂化。您可以在从旧文件中读取行时写入新文件。
import csv
with open('new_format.csv', 'w') as csvfile:
fieldnames = ['id', 'status', 'date']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
for row in file:
....
....
....
[writer.writerow({'id': id, 'status': 'OPEN', 'date': d}) for d in open_days]
[writer.writerow({'id': id, 'status': 'CLOSED', 'date': d}) for d in closed_days]
[writer.writerow({'id': id, 'status': 'CHECKED', 'date': d}) for d in checked_days]
Andrew 的回答并不是 100% 适合我,但在我做了一些小改动之后:
import csv
with open('new_format.csv', 'w') as csvfile:
fieldnames = ['id', 'status', 'date']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
for row in file:
....
....
....
writer.writerows({'id': id, 'status': 'OPEN', 'date': d} for d in open_days)
writer.writerows({'id': id, 'status': 'CLOSED', 'date': d} for d in closed_days)
writer.writerows({'id': id, 'status': 'CHECKED', 'date': d} for d in checked_days)
确实如此,而且看起来几乎正确(我不知道为什么我得到空白行):
...
8: 4882410,CLOSED,2016-11-09
9:
10: 4882410,CLOSED,2016-11-10
11:
12: 4882410,CLOSED,2016-11-11
...