Skip to content

Instantly share code, notes, and snippets.

@dogrdon
Created April 5, 2016 19:36
Show Gist options
  • Save dogrdon/481ac0fdcefb7c319a52e04b87fac95c to your computer and use it in GitHub Desktop.
Save dogrdon/481ac0fdcefb7c319a52e04b87fac95c to your computer and use it in GitHub Desktop.
def leftJoinCSV(file1, file2, *args):
'''Really should only be used to left join two csv files. Left file is first arg, right file is second arg.
Column(s) title (string in the head) are the third and fourth args'''
if len(args) > 1:
'''can either enter one column for both or each one '''
column1 = args[0]
column2 = args[1]
elif len(args) == 1:
column1 = args[0]
column2 = args[0]
f1csv = giveMeCSV(file1)
f2csv = giveMeCSV(file2)
f1head = next(f1csv)
f2head = next(f2csv)
f1Idx = getHeaderIndex(f1head)
f2Idx = getHeaderIndex(f2head)
newFile = makeNewFile(file1)
merged = csv.writer(open(newFile, 'w'))
merged.writerow(f1head+f2head)
right_indexed = {row[f2Idx[column2]].strip(): row for row in f2csv}
for row1 in f1csv:
if row1[f1Idx[column1]].strip() in right_indexed.keys():
mrow = row1 + right_indexed[row1[f1Idx[column1]]]
else:
mrow = row1
merged.writerow(mrow)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment