-3

I am trying to get the first word in a line (e.g. apple, book, chair). I have a long list of French words and their meanings in English:

French,English
partie,part
histoire,history
chercher,search
seulement,only
police,police
...

This list is stored in a csv. I want to get a random French word from this list. Does anybody know how I can do this?

My code(currently):

import random
with open('data/french_words.csv', 'r') as french_words:
    french_words_list = french_words.readlines()
rand_french_word = random.choice(french_words_list)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685

2 Answers2

2

You can use split:

import random

with open("words.txt") as f:
    next(f) # skip one line (i.e., the header)
    words = [line.split(',')[0] for line in f]

random_word = random.choice(words)
print(random_word) # histoire
j1-lee
  • 13,764
  • 3
  • 14
  • 26
0

Load the csv into a DataFrame if the list is big

import pandas as pd

df = pd.read_csv('french_words.csv')
df['French'].sample()
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Srikar Manthatti
  • 504
  • 2
  • 15