1

I am doing coursework where it requires me to make a Quiz programme where an account for a user is created and stored to a CSV along with personal information. It then requires me to read from a CSV file and read the questions and answers for the module to run and then it stores the test data to the CSV where all the account information is stored.

I have successfully written to the CSV however I am having a problem with reading correctly. The CSV is set up with 2 columns and 5 rows in A1, A2, A3, A4 and A5 is where the questions are stored and in B1, B2, B3, B4 and B5 is where the answers are stored for the question on the row. Currently I am attempting to print the Question into an input and then use an if statement to check it against the answer I have stored in the CSV.

Currently I am having problems with printing everything within the cell and I am having to use ranges. A section of my python code is as follows...

import csv

with open("questions.csv", 'r') as f:
   for row in f:
       question1 = row[0:94]
       answer1 = row[95]




print(question1)
print(answer1)

How do I print the whole cell into the input without having to do the ranges in the row?

2

2 Answers 2

2

You have imported the csv module but you never use it, try this:

import csv

with open('questions.csv', 'r') as f:
   reader = csv.reader(f)
   for row in reader:
      print('Question: {}'.format(row[0]))
      print('Answer: {}'.format(row[1]))
Sign up to request clarification or add additional context in comments.

Comments

0

in your code, row is a string containing all the cells in the current row separated by comma.

You want to do:

row = row.split(',').rstrip()

So now row[0] will be the whole content of the first col and so on....

Or you can use csv.reader as suggested by @Burhan Khalid

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.