Python: how to get the weekday from a CSV?
Question: I have a sample of CSV which contains various columns and i have to extract
the Weekday from the given `date` column. the sample is shown as below:

    
    
    Name,Birthdate,Age,Address
    ABC,3-10-2016 11:00:00AM,21,XYZ Street 21 zone
    BCD,3-11-2016 15:54:00PM,22,WXY Street 21/A, S zone
    CDW,4-11-2015 21:09:00PM,22,ZYX Street 21Avenue, North Zone
    

i want to read the CSV and extract the Date to determine the weekday of the
given date column

so far i have created a code to read the CSV and get the weekday but i am
unable to implement it on any other CSV.

The code is given below:

    
    
    import csv
    from datetime import datetime as dt
    with open('date.csv', 'r') as f:
      f.readline()
    
      for line in f:
        date = dt.strptime(line.strip(), '%m-%d-%Y %H:%M:%S').strftime('%A')
        print date
    

please help me here as this is a part of my academic research.

NOTE: In case if the question is not clear then please let me know. :)


Answer: Use the [`csv` module](https://docs.python.org/2/library/csv.html) to read CSV
files, then parse the one column. Since you have a file with headers, it'd be
easiest to use the `DictReader()` approach here:

    
    
    import csv
    from datetime import datetime
    
    
    with open(filename, 'rb') as infile:
        reader = csv.DictReader(infile)
        for row in reader:
             birthdate = row['Birthdate']  # keys are named in the first row of your CSV
             birthdate = datetime.strptime(birthdate, '%m-%d-%Y %H:%M:%S')
             print birthdate.strftime('%A')
    

