【发布时间】:2015-09-29 16:48:41
【问题描述】:
我正在尝试使用 Python 生成一个 csv,显示 5 名员工的每月费用数据。以下是我的代码:
#!/usr/bin/env python
import csv
import math
emp_id = 0
year = 2017
start_month = 1
months_to_add = 12
Amount = 50.00
emp_per_month = 5
output_file = 'Monthly.csv'
def format_emp_id(id):
return "E%03d" % (id,)
with open(output_file, 'wb') as csvfile:
writer = csv.writer(csvfile,
delimiter='\t',
quotechar='"',
quoting=csv.QUOTE_MINIMAL)
writer.writerow(['ID', 'Date', 'Amount'])
for month in range(start_month, months_to_add + 1):
date = "%d-%02.d-%02.d" % (year, month, 1)
for install in range(int(emp_per_month)):
emp_id += 1
writer.writerow([format_emp_id(emp_id), date, Amount])
emp_per_month = 5
但是,这段代码给了我以下输出:
ID | Date | Amount |
E001 | 2015-01-01 | 50.00 |
E002 | 2015-01-01 | 50.00 |
E003 | 2015-01-01 | 50.00 |
E004 | 2015-01-01 | 50.00 |
E005 | 2015-01-01 | 50.00 |
E006 | 2015-02-01 | 50.00 |
E007 | 2015-02-01 | 50.00 |
E008 | 2015-02-01 | 50.00 |
E009 | 2015-02-01 | 50.00 |
E010 | 2015-02-01 | 50.00 |
但是,这不是我需要它做的。我需要的是员工 ID E001-E005 每月重复一次,而不是继续增加员工 ID 超过 E005。
请就纠正此问题所需的修订告诉我。
【问题讨论】: