jeudi 30 mars 2017

Taking a sample of a CSV?

I have a program converting a CSV to a valid JSON file.

I would like to change it to take a random subset of my CSV file (as its 50k lines) and make a JSON from this. Here is my code so far -

import csv
import json

csvfile = open('C:\\Users\\ADMIN\\Desktop\\CSV_1.csv', 'r')
jsonfile = open('C:\\Users\\ADMIN\\Desktop\\Testing\\file.json', 'w')

with open('C:\\Users\\ADMIN\\Desktop\\CSV_1.csv') as f:
    reader = csv.DictReader(f)
    rows = list(reader)

with open('C:\\Users\\ADMIN\\Desktop\\Testing\\file.json', 'w') as f:
    json.dump(rows, f)

What's the most efficient way to do this?

Thanks.




Aucun commentaire:

Enregistrer un commentaire