First of all, here's the code I'm working on:
def get_data(filename, cols):
with codecs.open(filename, 'r') as csvFile:
for row in csv.reader(csvFile):
# build data dictionnary
data = {}
for i in range(len(cols)):
key = cols[i]
# update
value = row[i].strip()
# because some columns are splited in the csv file
if key in data:
data[key] += value
else:
data[key] = value
# yield data, for each row
yield data
So, depending of the size of the file, the time to process can be very long.
I tried testing csv.reader with custom dictionnary construction VS basic csv. DictReader, and for some reason, the first one is faster...
I have some long csv files with rows like this one:
"152Q694 ","892-000357 ", 0, 0," "
In some files, I have this (see the product name split in two columns?):
"A","COMPANY NAME ","1234","987654321 ","I AM A PRODUCT NAME ","WITH SOME EXTRA INFO ","AB ","12345 ",0000000000000001.23,0000000000000003.45,"A"," ","Z","01234567891234058","1234","EN",000000.01,"ABC","D"," "," ",000000,000000," "
All in all, there will always be n rows and x columns, so the time of the process is a multiple of n*x. Am I wrong?
What can I do to speed things up?
csv
module does not support Unicode input, so don't usecodecs.open
. \$\endgroup\$