vendredi 21 février 2020

Python csv random select rows and repeat by X number of times

Im new to Python and created a script that picks out random rows from a csv with two columns. But it only picks out one random row from each column once. This is my code:

import csv
import random

with open('Sheettwo.csv', 'r') as csv_file:
    csv_reader = csv.reader(csv_file)

    chosen_row = random.choice(list(csv_reader))

    paraone = (chosen_row[1])
    paratwo = (chosen_row[2])

    print(f'{paraone}\n{paratwo}\n')

I need to repeat this X number of times so (for example) 4 values are randomly selected (two from each column). How do i do that?




Aucun commentaire:

Enregistrer un commentaire