Home > Back-end >  How to read a csv file with 2 column and select a random line
How to read a csv file with 2 column and select a random line

Time:06-21

Hello i have csv file with quotes dataset,

col 1 = quotes
col 2 = authors

then i want to select randomly to put on:

cquote=(Q),
cauthor=(A),

and this code ive try.. sorry im new in python. thx for help

from Quote2Image import convert
import csv

with open('quotes.csv', encoding="utf8") as f:
    csv_reader = csv.DictReader(f)
    for line in csv_reader:
        print(f"{line['quotes']}")
        print(f"{line['authors']}")

Q = (f"{line['quotes']}")
A = (f"{line['authors']}")

img=convert(
    cquote=(Q),
    cauthor=(A),

CodePudding user response:

Maybe first read all rows to list

all_rows = list(csv_reader)

and later use module random to select random row from this list

random_row = random.choice(all_rows)

print(random_row['quotes'])
print(random_row['authors'])

Q = random_row['quotes']
A = random_row['authors']

import csv
import random
#from Quote2Image import convert

with open('quotes.csv', encoding="utf8") as f:
    csv_reader = csv.DictReader(f)
    all_rows = list(csv_reader)
    
# --- later ---

random_row = random.choice(all_rows)
    
print(random_row['quotes'])
print(random_row['authors'])

Q = random_row['quotes']
A = random_row['authors']

#img = convert(cquote=Q, cauthor=A, ...)
#img = convert(cquote=random_row['quotes'], cauthor=random_row['authors'], ...)

And when you have all lines in list then you can run for-loop to get more random rows

for _ in range(5):
    random_row = random.choice(all_rows)
        
    print(random_row['quotes'])
    print(random_row['authors'])
    
    Q = random_row['quotes']
    A = random_row['authors']

OR you can use other function to select many rows at start - and it will not repeate rows.

many_random_rows = random.sample(all_rows, k=5)

for random_row in many_random_rows:
    random_row 
        
    print(random_row['quotes'])
    print(random_row['authors'])
    
    Q = random_row['quotes']
    A = random_row['authors']

Minimal working code with example data as text in code - so everyone can simply copy and run it.
I use io only to simulate file in memory but you can use open() to read your file.

import csv
import random
import io

text = '''quotes,authors
"Hello World!","Python"
"Other text","Other authors"
"Second text","Second authors"
"Third text","Third authors"
"Fourth text","Fourth authors"
"Fifth text","Fifth authors"
'''

with io.StringIO(text) as f:
#with open('quotes.csv', encoding="utf8") as f:
    csv_reader = csv.DictReader(f)
    all_rows = list(csv_reader)
    
random_row = random.choice(all_rows)

Q = random_row['quotes']
A = random_row['authors']
    
print("Q:", Q)
print("A:", A)

print('--- many rows ---')

many_random_rows = random.sample(all_rows, k=3)
    
for random_row in many_random_rows:    
    Q = random_row['quotes']
    A = random_row['authors']
    print("Q:", Q)
    print("A:", A)
    print('---')
  • Related