My script reads two .csv files and generates one dictionary per .csv file.
import csv
# First dictionary
first_dict = {}
with open(first_file, 'r') as f:
csvReader = csv.reader(f)
next(csvReader, None) # skip the header
for row in csvReader:
key = row[0]
first_dict[key] = row[1]
# Second dictionary
second_dict = {}
with open(second_file, 'r') as f:
csvReader = csv.reader(f)
next(csvReader, None) # skip the header
for row in csvReader:
key = " ".join(row[:3]).replace(" "," ")
second_dict[key] = row[4]
Both reading procedures only differ in the generation of the key. While for first_dictionary
the key is just the first row, it is the first three rows for second_dictionary
. Is there a way to combine both procedures in one function while only setting appropriate arguments?