I'm trying to split a CSV file into multiple files. The code works fine if I use it from command line.
I'm running Python csv_splitterFunction.py C:\Users\xlViki\Desktop\Python\Journal.py in cmd
import sys
number_of_outfiles = 4
if __name__ == "__main__":
k = []
for i in range(number_of_outfiles):
k.append(open(r'C:\Users\xlViki\Desktop\Python\Output_' + str(i) + '.csv','w'))
with open(sys.argv[1]) as inf:
for i, line in enumerate(inf):
if line[-1] == '\n': line = line[:-1]
if i == 0:
headers = line
[x.write(headers + '\n') for x in k]
else:
k[i % number_of_outfiles].write(line + '\n')
[x.close() for x in k]
But I'm getting a list out of range error when I'm trying to convert the code into a function like below and running in the Python shell (pressing F5 in IDLE):
def Main(filepath):
import sys
number_of_outfiles = 4
if __name__ == "__main__":
k = []
for i in range(number_of_outfiles):
k.append(open(r'C:\Users\xlViki\Desktop\Python\Output_' + str(i) + '.csv','w'))
print (r'C:\Users\xlViki\Desktop\Python\Output_' + str(i))
with open(filepath) as inf:
for i, line in enumerate(inf):
if line[-1] == '\n': line = line[:-1]
if i == 0:
headers = line
[x.write(headers + '\n') for x in k]
else:
print(i)
print(k[0])
k[i % number_of_outfiles].write(line + '\n')
[x.close() for x in k]
Main(r'C:\Users\xlViki\Desktop\Python\Journal.csv')
This is the output I'm receiving:
C:\Users\xlViki\Desktop\Python\Output_0
1
<_io.TextIOWrapper name='C:\\Users\\xlViki\\Desktop\\Python\\Output_0.csv' mode='w' encoding='cp1252'>
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
Main(r'C:\Users\xlViki\Desktop\Python\Journal.csv')
File "C:\Users\xlViki\Desktop\Python\csv_splitterFunction.py", line 22, in Main
k[i % number_of_outfiles].write(line + '\n')
IndexError: list index out of range
Can you please help me convert this code into a function?