0

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?

2 Answers 2

1

The error message is telling you that the list index out of range exception occurred on line 22, k[i % number_of_outfiles].write(line + '\n'). This line is going to try and access k[0], k[1], k[2], and k[4], but on the first iteration through the loop you have only put 1 element into k, so you can only access k[0]. The solution to this is to open all of the files and put them in k before you enter the loop starting with with open(filepath) as inf:. Additionally, you have used i as a variable twice, if you intend to keep the loops nested you should change one of their names, otherwise you can just unindent with open(filepath) as inf:.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! that did it. Now when I know the answer, the difference in both codes looks so obvious. Thanks for the explanation as well.
1

Looks like you need to unindent everything from with open(filepath) as inf: on. The first file is opening, but then the second line attempts to write to the second file before it's even open.

1 Comment

Thanks a lot! That was it, I unindented the code and it just works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.