我有一个 csv 文件,排列如下:
Person,Date1,Date2,Status
Person1,12/10/11,17/10/11,Done
...
我想对其执行各种操作,首先将其拉入 Python 并将日期字符串转换为 datetime.datetime 对象。我有以下代码:
import re
import numpy as np
from datetime import datetime, timedelta
from dateutil import rrule
def get_data(csv_file = '/home/garry/Desktop/complaints/input.csv'):
inp = np.genfromtxt(csv_file,
delimiter=',',
filling_values = None,
dtype = None)
date = re.compile(r'\d+/\d+/\d+')
count = 0
item_count = 0
for line in inp:
for item in line:
if re.match(date, item):
item = datetime.strptime(item, '%d/%m/%y')
inp[count][item_count] = item
item_count += 1
else:
item_count += 1
item_count = 0
count += 1
return inp
def get_teams(data):
team_list = []
for line in data:
if line[0] not in team_list:
team_list.append(line[0])
else:
pass
del team_list[0]
return team_list
def get_months():
month_list = []
months = [1,2,3,4,5,6,7,8,9,10,11,12]
now = datetime.now()
start_month = now.month - 7
for count in range(0,7):
if months[start_month] > now.month:
year = now.year - 1
else:
year = now.year
month_list.append([months[start_month], year])
start_month += 1
return month_list
if __name__ == "__main__":
inp = get_data()
for item in inp[2]:
print type(item)
team_list = get_teams(inp)
month_list = get_months()
main 方法中的 print 语句(插入用于调试)返回:
<type 'numpy.string_'>
<type 'numpy.string_'>
<type 'numpy.string_'>
<type 'numpy.string_'>
这显然不是我所希望的,因为 get_data() 函数中的循环应该将日期字符串更改为 datetime.datetime 对象。当我在单个日期字符串上运行与循环中相同的代码作为测试时,它们会很好地转换类型。在上面的代码中,它们也在某种意义上起作用,因为字符串确实更改为 datetime.datetime 格式——它们只是不是正确的类型。谁能看到我在这里做错了什么?