I have a CSV file of 1 GB with around 1 million records, each row is 171 columns, I did some research and came up with this code. I have reduced the size of file to 5 MB for testing purposes, but there are still 171 columns. The code works fine as long as the sorting column index is below 50. Even on 49 it works fine, but I have columns with index 151, 153. I want to sort the file with those columns.
Error:
When I give it index 50 or above it throws the error:
data.sort(key=operator.itemgetter(*sort_key_columns))
IndexError: list index out of range
My Code:
def sort_csv(csv_filename, sort_key_columns):
data = []
with open(csv_filename, 'r') as f:
for row in csv.reader(f):
data.append(row)
data.sort(key=operator.itemgetter(*sort_key_columns))
with open(csv_filename, 'w', newline='') as f:
csv.writer(f).writerows(data)
sort_csv('Huge_Complete_B2B_file_1_1.csv', [49])