using python to randomize csv file
Question: I'm new for python and would love to get some help.

I have a csv file with 2 columns, think:

1 A

2 B

3 C

4 D

My aim is to use python to "open" the file, read it, randomize the order of
the two lists (i.e. have 1 be with the same line as C, 2 with D etc.), and
then save the new randomized lists in a different csv file.

Thanks,

Edit:

    
    
    import csv
    import random
    f=open('my_file.csv')
    csv_f=csv.reader(f)

I read some more stuff about writer. but unsure how to use these functions
yet..

edit 2: someone just posted a great comment, but for some reason I can't see
it now. the only problem is that I need to keep the columns headers intact,
they can't be randomized. The code was as follows:

    
    
    import csv
    import random
    
    with open ("my_file") as f:
      l=list(csv.rader(ff))
    
    random.shuffle(l)
    
    with open("random.csv", "W") as f:
      csv.writer(f).writerows(f)


Answer: You can read the rows as list, extract the two columns, then shuffle each one,
then zip the columns together and finally write the result to a new csv file:

    
    
    import csv
    import random
    
    with open("input.csv") as f:
        r = csv.reader(f)
        header, l = next(r), list(r)
    
    a = [x[0] for x in l]
    random.shuffle(a)
    
    b = [x[1] for x in l]
    random.shuffle(b)
    
    with open("random.csv", "wb") as f:
        csv.writer(f).writerows([header] + zip(a, b))
    

