我有一个给定的CSV样本如下:
ID,ID_TYPE,OB_DATE,VERSION_NUM,MET_DOMAIN_NAME,OB_END_CTIME,OB_DAY_CNT,SRC_ID,REC_ST_IND,PRCP_AMT,OB_DAY_CNT_Q,PRCP_AMT_Q,METO_STMP_TIME,MIDAS_STMP_ETIME,PRCP_AMT_J
90, RAIN, 2006-01-01 00:00,1, WADRAIN,900,1,24109,1011,0,0,6, 2006-01-17 09:04,0,
150, RAIN, 2006-01-01 00:00,1, DLY3208,900,1,30747,1011,0,0,6, 2006-01-09 13:21,3,
174, RAIN, 2006-01-01 00:00,1, WADRAIN,900,1,24775,1011,0.2,0,6, 2006-01-17 09:04,0,
我想在CSV中确定每个指定日期的工作日。我的代码实现如下:
import csv
from datetime import datetime as dt
csv_file = open('raindata.csv')
csv_reader = csv.DictReader(csv_file)
field_names = list(csv_reader.fieldnames)
if 'WEEKDAY' in field_names:
print "data has error"
elif 'RECWEEKDAY' in field_names:
print "data has error"
else:
field_names.insert(field_names.index('OB_DATE') + 1, 'WEEKDAY')
field_names.insert(field_names.index('METO_STMP_TIME') + 1, 'RECWEEKDAY')
def get_weekday(ob_date):
return dt.strptime(ob_date, ' %Y-%m-%d %H:%M').strftime('%A')
output = open('raindata.csv','w')
csv_writer = csv.DictWriter(output, field_names)
csv_writer.writeheader()
for row in csv_reader:
row['WEEKDAY'] = get_weekday(row['OB_DATE'])
row['RECWEEKDAY'] = get_weekday(row['METO_STMP_TIME'])
csv_writer.writerow(row)
我的脚本运行正常并提供了正确的结果,但在 OB_DATE 列和 METO_STMP_TIME 列中缺少Date
值时失败。
如何更改现有代码,以便对于空白Date
值,相应的Weekday
值也为空?
答案 0 :(得分:2)
只捕获日期/时间字符串丢失或无效时引发的异常,然后将该值设置为空字符串。
try:
row['WEEKDAY'] = get_weekday(row['OB_DATE'])
except ValueError:
row['WEEKDAY'] = ''
答案 1 :(得分:2)
对于其他选择,您可以修改get_weekday
功能以处理空白日期。
def get_weekday(ob_date):
return dt.strptime(ob_date, ' %Y-%m-%d %H:%M').strftime('%A') if ob_date.strip() else ""