Home > Back-end >  about open csv file and read line by line in and choice randomly python
about open csv file and read line by line in and choice randomly python

Time:10-31

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

CodePudding user response:

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.

CodePudding user response:

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.

  • Related