简体   繁体   中英

How do I separate an excel file in python?

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

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.

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