我有一个以下格式的文件:
name date
sam 21/1/2003
bil 5/4/2006
sam 4/7/2009
Mali 24/7/2009
bil 13/2/2008
etc...
我想设置一个固定的日期,例如:1/1/2003,从我的固定日期中减去所有的日期,然后除以周,找出哪些名字是在哪个星期注册的,然后把它们放在一个集合中。所以我想得到以下最终结果:
^{pr2}$我已经编写了下面的python脚本,但它不起作用。我有以下错误:
val=set(start_date-'date(data.files.datetime)')
TypeError: unsupported operand type(s) for -: 'int' and 'str'
有人知道为它编写代码的最佳方法是什么吗?在
import pprint
import csv
with open('d:/Results/names_info.csv', 'r') as csvfile:
start_date= 1/1/2003
filereader=csv.reader(csvfile,'excel')
for row in filereader:
for name in row:
key=name
val=set(start_date-'date(data.files.datetime)')
datedict[key]=val
pprint.pprint (datedict)
您的代码中有几个错误:
date
类型。在datedict
中的项,而不首先检查它们是否存在。在以下是修复这些错误后代码的外观:
打印出来:
^{pr2}$你应该能想出办法让它打印“周”。在
您肯定希望使用标准库中的
datetime
模块。计算周差的一种快速而肮脏的方法可以是:另外,如果您想要一个
^{pr2}$dict
的list
,请将datedict[key]=val
替换为另外,如果您希望列表具有格式为week1、week12等的字符串,那么只需使用
相关问题 更多 >
编程相关推荐