0

csv1 and csv2 have the first column in common and don't have headers. I'm trying to combine them in a single output file with headers, ID, Item, and Price. The price is not in order in csv2, so I want the program to match the IDs and output the corresponding prices.

csv1

1   A
2   B
3   C

csv2

3  10
1  15
2  8

Desired Output

ID  Item Price
1   A    15  
2   B    8
3   C    10

This is my code so far. I can't figure out how I should do the rest. I'm a beginner and I want to do this without pandas or any other modules.

import csv

with open('output.csv', 'w', newline="") as FileN:
    output = csv.DictWriter(FileN, fieldnames=["ID", "Item", "Price"])
    output.writeheader()

    with open("csv1.csv", "r") as File1:
        input1 = csv.reader(File1)

        for row in input1:
            output.writerow({"Item ID": row[0], "Item": row[1]})

       
noob101
  • 1
  • 1
  • Are you using a database at all or must this be done entirely in Python? Perhaps https://stackoverflow.com/questions/16265831/merging-two-csv-files-using-python will help. – sativay Nov 16 '20 at 12:46
  • no database. Entirely python. I'll check that out – noob101 Nov 16 '20 at 22:22

0 Answers0