-1

I'm having trouble take a excel file that question number in column 1, the question in column 2, and in column 3,4,5,6 having the answer options for that question. How do I separate these? This is what I have to read the file

WriteFile=open('user_database.csv','a')

questionfile= "database.csv"
readfile=open(questionfile, "r")
questions=readfile.readlines()
askedquestions=[]
anwsers=[]
score=0
  • 1
    Possible duplicate of [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) – wjandrea Apr 28 '19 at 21:12
  • Are you trying to divide the file into 2 files ? One with questions and one with answers? – francovici Apr 28 '19 at 23:32

1 Answers1

0

I'm assuming you want to write the answers into a different file. I'm taking for granted your files are separated by ';' because they're .csv files.

Since I'm not entirely sure on what you want to do with your data, I'm just gonna show you what I would do to get the questions and answers separated from the file and then writing the answers into the output file. Then you can do what you want with the information I gave you.

WriteFile=open('user_database.csv','a')

questionfile= "database.csv"

readfile=open(questionfile, "r")

for line in readfile:
   question = line.split(';')
   q_number = question [0]
   q_text = question [1]
   q_answer_option_1 = question [2]
   q_answer_option_2 = question [3]
   q_answer_option_3 = question [4]
   q_answer_option_4 = question [5]

   #writing answers into the file:
   output_line= q_number+';'+q_answer_option_1+';'+q_answer_option_2+';'+q_answer_option_3+';'+q_answer_option_4+"\n"
   WriteFile.write(output_line)

readfile.close()
WriteFile.close()

What I'm doing here is extracting the data inside the file into variables and writing the output file with the format : "Question_number;Answer_Option_1;Answer_Option_2;Answer_Option_3;Answer_Option_4"

If you're aiming for something different, I'll edit the answer for you.

Hope it helps.

francovici
  • 536
  • 6
  • 14