我正在生成一个 python 脚本来快速创建一个 csv 表来记录员工薪酬数据。
下表是员工 ID 001 的 2015 年薪资表示例。对于薪资周期 2,即二月、周期 6(六月)、周期 9(九月)——我们发放奖金,反射(reflect)在“金额”列中.
ID | Description| Date | Status | Amount
001 | Pay period | 1/31/2015 | 1 | 50.00
001 | Pay period | 2/28/2015 | 1 | 100.00
001 | Pay period | 3/31/2015 | 1 | 50.00
001 | Pay period | 4/40/2015 | 1 | 50.00
001 | Pay period | 5/31/2015 | 1 | 50.00
001 | Pay period | 6/30/2015 | 1 | 100.00
001 | Pay period | 7/31/2015 | 1 | 50.00
001 | Pay period | 8/31/2015 | 1 | 50.00
001 | Pay period | 9/30/2015 | 1 | 100.00
001 | Pay period | 10/31/2015| 1 | 50.00
001 | Pay period | 11/30/2015| 1 | 50.00
001 | Pay period | 12/31/2015| 1 | 50.00
我希望能够使用这种结构快速生成一个 csv,自动为每个月写入行,并考虑到二月、六月和九月的特殊奖金支付期。
我的代码是这样的,但我无法进一步考虑特殊月份的不同处理方式,以及如何自动计算每个月的最后一天作为自动增量:
#!/usr/bin/env python
import csv
ID = 0
month = 0
year = 2015
with open('Employee.csv', 'wb') as csvfile:
writer = csv.writer(csvfile, delimiter='\t', quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow(['ID', 'description', 'date', 'status', 'amount'])
for month in range(1, 13):
date = "%d-%02.d-%02.d" % (year, month, 1)
for install in range(100):
Pay Period += 1
writer.writerow([ID(ID), 'Pay period', date, install_fee])
请您参考如下方法:
下面的代码使用了monthrange从日历中获取该月的最后一天。
我不确定你想为奖金月做什么,所以我现在只用 pass 填充它。
from calendar import monthrange
with open('Employee.csv', 'wb') as csvfile:
writer = csv.writer(csvfile,
delimiter='\t',
quotechar='"',
quoting=csv.QUOTE_MINIMAL)
writer.writerow(['ID', 'description', 'date', 'status', 'amount'])
for month in range(1, 13):
date = "%d-%02.d-%02.d" % (year, month, monthrange(year, month)[1])
if month in [2, 6, 9]: # Special months
pass # Add bonus
for install in range(100):
Pay Period += 1
writer.writerow([ID(ID), 'Pay period', date, install_fee])
