Bat
Bat

Reputation: 155

shuffle rows of csv with python

i would like to simply shuffle the rows of a csv file but keep the headers static.

contents of unshuffled.csv

Lastname  Firstname   Age

taylor    bob         40
mcdonald  kevin       32
smith     john        18

would like to output to shuffled.csv as below

Lastname  Firstname   Age
smith     john        18
mcdonald  kevin       32
taylor    bob         40

I am using code below, which was suggested in another post, but doesnt work for me.

from random import shuffle

with open('unshuffled.csv','r') as ip:
    data=ip.readlines()

    header, rest=data[0], data[1:]

    shuffle(rest)
with open('shuffled.csv','w') as out:
    out.write(''.join([header]+rest))

The output csv however shuffles data outside of the three columns as below.

Lastname  Firstname   Age
smith     john        18    32    kevin
taylor    bob         40

How can I make the columns static and just shuffle the rows in the csv file.

Upvotes: 0

Views: 2342

Answers (2)

zipa
zipa

Reputation: 27879

You must be missing the newline character on the last row of unshuffled.csv, so use something like this:

import random

with open('unshuffled.csv', 'r') as r, open('shuffled.csv', 'w') as w:
    data = r.readlines()
    header, rows = data[0], data[1:]
    random.shuffle(rows)
    rows = '\n'.join([row.strip() for row in rows])
    w.write(header + rows)

Upvotes: 1

Umair Mohammad
Umair Mohammad

Reputation: 4635

Try something like this:

from random import shuffle

with open('unshuffled.csv') as ip:
    lines=ip.readlines()
    header = lines.pop(0)
    shuffle(lines)
    lines.insert(0, header)

with open('shuffled.csv','w') as out:
    out.writelines(lines)

Upvotes: 0

Related Questions