I have two CSV files that each have two columns, id and name. I want to compare both files by their name columns; if the values match, then create a new CSV file with the id values from both files.
1.csv:
id, name
1, sofia
2, Maria
3, sofia
4, Laura
2.csv:
id, name
1, sofia
2, Laura
My code:
import csv
with open('1.csv') as companies, open('2.csv') as tags:
companies = companies.readlines()
tags = tags.readlines()
with open('CompanieTags.csv', 'w') as outFile:
for line in companies:
if line[1] != tags[1]:
line2 = companies[1]
outFile.write(line[0] and linea2)
Other code with Dict's
import csv
with open('1.csv') as companies, open('2.csv') as tags:
reader = csv.DictReader(companies)
check = csv.DictReader(tags)
with open('CompanieTags.csv', 'w') as outFile:
for x in check:
SaveTag = x['name']
for y in reader:
if SaveTag in y['name'] :
outFile.write(y['id'], x['id'])
Expected result:
id, name
1, 1
3, 1
4, 2