Skip to content

Instantly share code, notes, and snippets.

@nickyfoto
Created August 29, 2015 14:04
Show Gist options
  • Save nickyfoto/1d6b3a5dae0bb1ab6eb1 to your computer and use it in GitHub Desktop.
Save nickyfoto/1d6b3a5dae0bb1ab6eb1 to your computer and use it in GitHub Desktop.
from time import strptime, strftime
d = "2013-08-12"
oldFormat = "%Y-%m-%d"
newFormat = "%d-%b-%Y"
oldDate = strptime(d, oldFormat)
# print oldDate
# print strftime(newFormat, oldDate).upper()
infile = open('tobeconvert.csv', 'r')
outfile = open('new.csv', 'w')
for line in infile:
first = line.split(',')[0]
second = line.split(',')[1]
firstTimeTuple = strptime(first, oldFormat)
new_first = strftime(newFormat, firstTimeTuple).upper()
if second != "\n":
secondTimeTuple = strptime(second[:10], oldFormat)
new_second = strftime(newFormat, secondTimeTuple).upper() + '\n'
else:
new_second = "\n"
newline = new_first + ',' + new_second
outfile.write(str(newline))
outfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment