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?

Luke B
  • 129
  • 3
  • 9
  • 1
    Can you please edit the question to have dummy data of your questions.csv – kingmakerking Nov 14 '17 at 10:41
  • Does this answer your question? [How do I read and write CSV files with Python?](https://stackoverflow.com/questions/41585078/how-do-i-read-and-write-csv-files-with-python) – Mateen Ulhaq Jul 29 '22 at 08:13

2 Answers2

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]))
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
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