I have a list of integers saved in a csv sheet, the rows are not all the same length. Like the following example:
22,-14,-24,2,-26,18,20,-4,12,16,8,-6,-10
20,12,-16,18,28,24,4,-22,26,8,-10,-14,2,6
10,-26,-20,30,24,-22,18,-28,12,14,-6,-2,8,-16,-4
16, 22, 30, -18, -26, -28, 24, -8, 32, -14, 12, 4, 20, -10, 2, 6
32, 10, -14, 20, -22, 24, -4, -26, 34, 28, -30, 2, 12, 18, 6, -8, 16
8, -20, 34, 18, 30, 24, -4, 6, 28, -32, -12, -36, 10, 16, -38, 2, 14, -22, -26
I need to call a function where the input is an array consisting of one such row. So I need exactly the following.
input = [22,-14,-24,2,-26,18,20,-4,12,16,8,-6,-10]
Using the standard approach
import csv
with open(file.csv, 'r') as f:
reader = csv.reader(f)
for line in reader:
print(line)
yields the output
['22', '-14', '-24', '2', '-26', '18', '20', '-4', '12', '16', '8', '-6', '-10']
which I can't use since the elements are not integers. I have tried to use different formatting parameters, like csv.QUOTE_NONE but nothing works. This makes sense as far as I know since csv files do not know integer data types.
My files have between 100'000-1'000'000 rows so any solution must be efficient. Since the number of columns is not fixed I also was not able to cast manually, I couldn't figure out how to loop through the columns of one row. Does anyone have an idea how I could solve this problem? I don't know if it could help but I am not bound to csv files, I could probably use something else.
line = [int(x) for x in line]