So i have a big csv file and my code prints all the rows but i want to print, for example, only 20 random rows from 100000 rows. I know that somehow with random.sample
u can do that, but i don’t really know how. Any suggestions?
There is my code:
JavaScript
x
9
1
import csv
2
3
with open(r'Z:/**/**/**/test_examples_doors/
4
**') as csvfile:
5
data = csv.DictReader(csvfile)
6
for row in data:
7
if row['open']=='1':
8
print(row['image'], row['open'])
9
Advertisement
Answer
I assume you want to randomly sample your data, rather than just take the first 20 rows?
In this case you can convert data
to a list and then sample it:
JavaScript
1
6
1
import csv
2
import random
3
with open(r'Z:/datasets/room-segmentation/labeling/test_examples_doors/labels.csv') as csvfile:
4
data = csv.DictReader(csvfile)
5
sampled_data = random.sample(list(data), 20)
6