I have written code that reads in a CSV file and creates a tuple from all the items in a group. The group ID is in column 1 of the table and the item name is in column 2. The actual datafile is ~500M rows.
Is there any way to make this code more efficient?
Input file:
"CustID"|"Event" 1|Alpha 1|Beta 1|AlphaWord 1|Delta 2|Beta 2|Charlie 2|CharlieSay
Code:
def sequencer(myfile):
import csv
counter = 1
seq = []
sequence = []
with open(myfile, 'rb') as csvfile:
fileread = csv.reader(csvfile, delimiter='|', quotechar='"')
next(fileread) ## skip header
for row in fileread:
#if counter == 5:
# break
if 'word' in row[1] or 'say' in row[1]: ##if event has either word or say anywhere in the text then ignore (eg: even ignore adword or afdjklSAYwer)
continue
if int(row[0]) == counter:
seq.extend([row[1]])
else:
sequence.append(seq)
seq = [row[1]]
counter = counter+1
sequence.append(seq)
return sequence
Output:
An array which is a list of lists, where each list is the sequence of events in the order of the text file for each customer ID.