简体   繁体   English

用python洗净csv行

[英]shuffle rows of csv with python

i would like to simply shuffle the rows of a csv file but keep the headers static. 我想简单地改组csv文件的行,但保持标头为静态。

contents of unshuffled.csv unshuffled.csv的内容

Lastname  Firstname   Age

taylor    bob         40
mcdonald  kevin       32
smith     john        18

would like to output to shuffled.csv as below 想要输出到shuffled.csv如下

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. 但是,输出csv会按如下所示在三列之外对数据进行混洗。

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. 我如何才能使列为静态,而只是将csv文件中的行洗牌。

You must be missing the newline character on the last row of unshuffled.csv , so use something like this: 您必须在unshuffled.csv的最后一行上缺少换行符,因此请使用以下命令:

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)

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM