lundi 5 septembre 2016

Exporting a random sample from CSV file to a new CSV file - output is messy

I am trying to export a random subset of a CSV file to a new CSV file using the following code:

with open("DepressionEffexor.csv", "r") as effexor:
    lines = [line for line in effexor]
    random_choice = random.sample(lines, 229)

with open("effexorSample.csv", "w") as sample:
   sample.write("\n".join(random_choice))

But the problem is that the output CSV file is very messy. for example, some part of a data in a filed was printed in the next line. How can I solve the problem? In addition, I want to know how can I use pandas for this problem rather than CSV. Thanks !




Aucun commentaire:

Enregistrer un commentaire