简体   繁体   中英

Python: How to handle missing values in a CSV?

I have a given CSV sample as follows:

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,

I would like to determine the weekday of each given date in my CSV. My code which achieves that looks as follows:

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)

My script runs fine and gives the correct result but it fails where the Date values are missing from OB_DATE column and METO_STMP_TIME column.

How do I change the existing code, so that for a blank Date value the corresponding Weekday value is also blank?

Just catch the exception that is thrown when the date/time string is missing or invalid and then set the value to an empty string.

try:
    row['WEEKDAY'] = get_weekday(row['OB_DATE'])
except ValueError:
    row['WEEKDAY'] = ''

For other alternative, you can modify your get_weekday function to handle a blank date.

def get_weekday(ob_date):
    return dt.strptime(ob_date, ' %Y-%m-%d %H:%M').strftime('%A') if ob_date.strip() else ""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM