PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 28, 2022

[FIXED] How can I export my quiz results to a csv file using python 3?

 August 28, 2022     csv, export-to-csv, python, python-3.x     No comments   

Issue

I want the results to be in the format Date, Question, Name and Score

file = open("results.csv", "a")
file.write('Date, Question, Name, Score\n' + date + ',' question + ',' + name + ',' + score + '\n')
file.close()

When I run this code i keep getting the error: TypeError: Can't convert 'int' object to str implicitly


Solution

You have to cast to any ints to string string before you can concat it to another and write to file.

str(score) #  <-

 file.write('Date, Question, Name, Score\n' + date + ',' question + ',' + name + ',' + str(score) + '\n')

Or use str.format:

with open("results.csv", "a") as f: # with closes your files automatically
    f.write('Date, Question, Name, Score\n {}, {}, {}, {}'.format(date, question, name ,score))

You may also find the csv module useful



Answered By - Padraic Cunningham
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing