Index(['Province/State', 'Country/Region', 'Lat', 'Long', '1/22/20', '1/23/20',
'1/24/20', '1/25/20', '1/26/20', '1/27/20', '1/28/20', '1/29/20',
'1/30/20', '1/31/20', '02-01-20', '02-02-20', '02-03-20', '02-04-20',
'02-05-20', '02-06-20', '02-07-20', '02-08-20', '02-09-20', '02-10-20',
'02-11-20', '02-12-20', '2/13/20', '2/14/20', '2/15/20', '2/16/20',
'2/17/20', '2/18/20', '2/19/20', '2/20/20', '2/21/20', '2/22/20',
'2/23/20', '2/24/20', '2/25/20', '2/26/20', '2/27/20', '2/28/20',
'2/29/20', '03-01-20', '03-02-20', '03-03-20', '03-04-20', '03-05-20',
'03-06-20', '03-07-20', '03-08-20', '03-09-20', '03-10-20', '03-11-20',
'03-12-20', '3/13/20', '3/14/20', '3/15/20', '3/16/20', '3/17/20',
'3/18/20', '3/19/20', '3/20/20', '3/21/20', '3/22/20', '3/23/20',
'3/24/20', '3/25/20', '3/26/20', '3/27/20', '3/28/20', '3/29/20',
'3/30/20', '3/31/20', '04-01-20', '04-02-20', '04-03-20', '04-04-20',
'04-05-20'],
dtype='object')
如何使用for循环将这些日期列转换为通用格式,即mmddddyy格式?
这个代码做了一些假设。
代码会遍历每一个字符串。我定义了2个模式(斜线日期模式和连字符日期模式)。我看到日期字符串通过了哪种模式。例如,如果日期字符串通过了斜线_date_pattern,我们知道日期字符串的格式是mmddyy。strptime需要0个填充的月份和0个填充的日期字段,特别是当你分别使用%M和%d标志的时候(参见 https:/docs.python.org3librarydatetime.html#strftime-and-strptime-format-codes。). 所以我把字符串传给clean_date_str,以进一步格式化字符串,使其可以传给strptime。
import datetime as dt
import re
dates = ['1/22/20', '1/23/20',
'1/24/20', '1/25/20', '1/26/20', '1/27/20', '1/28/20', '1/29/20',
'1/30/20', '1/31/20', '02-01-20', '02-02-20', '02-03-20', '02-04-20',
'02-05-20', '02-06-20', '02-07-20', '02-08-20', '02-09-20', '02-10-20',
'02-11-20', '02-12-20', '2/13/20', '2/14/20', '2/15/20', '2/16/20',
'2/17/20', '2/18/20', '2/19/20', '2/20/20', '2/21/20', '2/22/20',
'2/23/20', '2/24/20', '2/25/20', '2/26/20', '2/27/20', '2/28/20',
'2/29/20', '03-01-20', '03-02-20', '03-03-20', '03-04-20', '03-05-20',
'03-06-20', '03-07-20', '03-08-20', '03-09-20', '03-10-20', '03-11-20',
'03-12-20', '3/13/20', '3/14/20', '3/15/20', '3/16/20', '3/17/20',
'3/18/20', '3/19/20', '3/20/20', '3/21/20', '3/22/20', '3/23/20',
'3/24/20', '3/25/20', '3/26/20', '3/27/20', '3/28/20', '3/29/20',
'3/30/20', '3/31/20', '04-01-20', '04-02-20', '04-03-20', '04-04-20',
'04-05-20']
def clean_date_str(groups, separator):
day = groups[1]
month = groups[0]
year = groups[2]
#If the day field is a single digit
#To allow strptime to work properly
#We have to pad a 0 to the beginning
while len(day) != 2:
day = '0' + day
print(day)
#If the month field is a single digit
#To allow strptime to work properly
#We have to pad a 0 to the beginning
while len(month) != 2:
month = '0' + month
print(month)
# you can add padding for year as well.
# But given the data is as above
# there is no need to do so.
return '/'.join([month, day, year])
#This data structure holds the list of cleaned dates in mm/dd/yy format
cleaned_dates = []
slash_date_pattern = re.compile(r'([\d]+)/([\d]+)/([\d]+)')
hyphen_date_pattern = re.compile(r'([\d]+)-([\d]+)-([\d]+)')
for date_str in dates:
if slash_date_pattern.match(date_str):
slash_object = slash_date_pattern.match(date_str)
cleaned_dates.append(clean_date_str(slash_object.groups(), '/'))
elif hyphen_date_pattern.match(date_str):
hyphen_object = hyphen_date_pattern.match(date_str)
cleaned_dates.append(clean_date_str(hyphen_object.groups(), '-'))
#This data_structure holds the datetime() objects for each date string present in input
date_objects = []
for date_str in cleaned_dates:
print(dt.datetime.strptime(date_str, '%M/%d/%y'))
date_objects.append(dt.datetime.strptime(date_str, '%M/%d/%y'))
你可以使用strptime()将你的字符串转换为日期时间格式,并使用strftime()来获得所需的输出。