Skip to content

Instantly share code, notes, and snippets.

@baskaufs
Created February 19, 2019 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baskaufs/536c39dae360b0903c2ef3f7449d16a6 to your computer and use it in GitHub Desktop.
Save baskaufs/536c39dae360b0903c2ef3f7449d16a6 to your computer and use it in GitHub Desktop.
convert tape codes into database codes
import csv
monthDict = {'jan':'01', 'feb':'02', 'mar':'03', 'apr':'04', 'may':'05', 'jun':'06', 'jul':'07', 'aug':'08', 'sep':'09', 'oct':'10', 'nov':'11', 'dec':'12'}
def convertTapeCode(tape):
# has problems when the day doesn't have two digits
year = tape[7:11]
monthAbbrev = tape[0:3]
monthNumber = monthDict[monthAbbrev.lower()]
day = tape[4:6]
network = tape[12:15]
outData = year + monthNumber + day + network
return outData
def convertBySplit(tape):
list = tape.split(' ') # split the date string up at its spaces
year = list[2]
monthAbbrev = list[0]
monthNumber = monthDict[monthAbbrev.lower()]
day = list[1]
if len(day) == 1:
day = '0' + day
network = list[3]
outData = year + monthNumber + day + network
return outData
tapeList = ['Sep 4 1975 ABC', 'Apr 3 1980 CBS', 'Aug 14 1992 CNN']
dataBase = []
dataBase.append(['tape code','data code'])
for tape in tapeList:
answer = convertBySplit(tape)
list = [tape, answer]
dataBase.append(list)
fileObject = open('test.csv', 'w', newline='', encoding='utf-8')
writerObject = csv.writer(fileObject)
for row in dataBase:
print(row)
writerObject.writerow(row)
fileObject.close()
print(dataBase)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment