Split a huge CSV in three random files in Python
Question: I have a huge CSV and I want to split it in 3 random files with almost* equal
size.

*almost: the size cannot be divided by 3

I was thinking to create 3 blank lists, then in a for loop, I would randomly
choose one number between `range(0,len(mycsv))` and append it in each list.
Then, I will create a csv with the files from the first list and go on. But I
think that this will be slow enough. Is there any build-in way or an easier
than my own?


Answer: For each line of your csv, randomly insert this line in one of three blank csv
files. For 100.000 lines, it should not take long.

    
    
    import random
    
    with open("mycsv.csv") as fr:
        with open("1.csv", "w") as f1, open("2.csv", "w") as f2, open("3.csv", "w") as f3:
            for line in fr.readlines():
                rd = random.randint(1, 3)
                f = f1 if rd == 1 else (f2 if rd == 2 else f3)
                f.write(line)
    

