Due to a restriction on being able to use pandas (not allowed) I am trying to do a left join operation between two csv files. I'm struggling. Here is an example:
import csv
def read_csv(path):
file = open(path, "r")
content_list = []
for line in file.readlines():
record = line.split(",")
for item in range(len(record)):
record[item] = record[item].replace("\n","")
content_list.append(tuple(record))
return content_list
lookup_list = read_csv("lookup.csv")
data_list = read_csv("data.csv")
print("list with id and name:")
print(lookup_list)
print("list with id, age, weight:")
print(data_list)
result =list()
data_dict = {x[0]: x for x in data_list[1:]}
for left in lookup_list[1:]:
if left[0] in data_dict:
result.append(data_dict.get(left[0]) + left[1:])
print("Result of merge:")
print(result)
list with id and name:
[('id', 'name'), ('123', 'Robin'), ('221', 'Larry'), ('331', 'Wilson'), ('412', 'Jack')]
list with id, age, weight:
[('id', 'age', 'weight'), ('123', '47', '320'), ('221', '47', '190'), ('331', '25', '225'), ('412', '21', '180'), ('110', '14', '150')]
Result of merge:
[('123', '47', '320', 'Robin'), ('221', '47', '190', 'Larry'), ('331', '25', '225', 'Wilson'), ('412', '21', '180', 'Jack')]
As the lookup_list does not have an entry for id 110, it is not included in the results. I need it to be included in the results with an empty value for 'name'. This is where I'm struggling.
This was so much easier with pandas but our automation engineers are restricting us to only libraries/modules included with the standard python distribution.
Thanks in advance for your help.