I have 2 CSV files of varying lengths. I'm looking for a pythonic way to loop over both of them, pulling data from predefined columns and append that data to my 2 list respectively.
So data from f1 goes to list_A and data from f2 goes to list_B
I realise using zip wont work as the number of rows in each file is different.
list_A = []
list_B = []
with open(file_1,'r') as f1, open(file_2,'r') as f2:
csvFile_1 = csv.reader(f1, delimiter=',')
csvFile_2 = csv.reader(f2, delimiter=',')
for x,y in zip(csvFile_1, csvFile_2):
list_A.append(x[1])
list_B.append(y[0])
Any pointers appreciated..
CodePudding user response:
There is no reason to read them with zip
, read them independently
with open(file_1, 'r') as f1, open(file_2, 'r') as f2:
list_A = [x[1] for x in csv.reader(f1, delimiter=',')]
list_B = [x[0] for x in csv.reader(f2, delimiter=',')]
Or use pandas
library
import pandas as pd
list_A = pd.read_csv(file_1, sep=',').iloc[:, 1].to_list()
list_B = pd.read_csv(file_2, sep=',').iloc[:, 0].to_list()