Skip to content Skip to sidebar Skip to footer

Python: How To Add Values Obtained From An Operation In A New Column Of Given Csv?

i have a sample CSV given as follows: Name,Birthdate,Age,Address,Zone 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-2

Solution 1:

Just for fun

import re
from datetime import datetime as dt

def changedate(birthdate):
    weekday = dt.strptime(birthdate, '%m-%d-%Y %H:%M:%S%p').strftime('%A')
    return birthdate + ',' + weekday

str = open(filename).read()
str = re.sub(r'Name,Birthdate,Age,Address','Name,Birthdate,Weekday,Age,Address', str)
str = re.sub(r'(\d+-\d+-\d+\s\d+:\d+:\d+\w{2})',lambda m: changedate(m.group()), str)

with open(filename, 'w') as f:
    f.write(str)

Post a Comment for "Python: How To Add Values Obtained From An Operation In A New Column Of Given Csv?"