简体   繁体   中英

about open csv file and read line by line in and choice randomly python

I have a csv file that contains questions, options and answers. I want to open that file in a class called quiz to show the question and answer randomly and the user enters the answer And compare the answers

Hi thanks for asking the question. It is better to use the json file structure for the quiz game. for example

{
    '1': {
        "question": "What is the capital of India?",
        "option": ['New Delhi', 'Abu Dhabi'],
        "answer": "New Delhi"
    },
}

It will loaded in dict format and help you to render through django.

python csv module can be of use for you. for more convenience, you can use pandas. it gives you more utilities to work with a csv file. randomness can be achieved via python random module.

if using pandas, having your data loaded into an object called df, the following might help:

question = df.sample()

this would randomly return one row of your data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM