我的.csv
数据如下所示:
June 8, 2009 Monday
June 8, 2009 Monday
June 6, 2009 Saturday
June 6, 2009 Saturday Correction Appended
June 6, 2009 Saturday
June 6, 2009 Saturday
June 6, 2009 Saturday
etc...
数据跨越10年。我需要将月份和年份分开(并且不要关心日期和日期)。
要说出几个月,我有下一行代码:
for row in reader:
date = row[1]
month = date.partition(' ')[0]
print month
但是,我无法弄清楚如何从字符串中提取数字年份?我是否必须使用正则表达式?
答案 0 :(得分:5)
尝试:
for row in reader:
row_split = row[1].split()
month = row_split[0]
year = int(row_split[3])
row[1] == "June 8, 2009 Monday"
因此:
row[1].split() == ["June", "8,", "2009", "Monday"]
因此,您的月份和年份提取如下:
"June" == row[1].split()[0]
2009 == int(row[1].split()[2])