VortexHD
VortexHD

Reputation: 9

write() argument must be str, not list

I'm trying to write a register program using python 3.6.2 and i'm getting the following error: write() argument must be str, not list

Here is the code:

database = open('database.csv', 'a')
linetowrite = ['username', 'password', 'forename', 'surname', 'dob', 'artist', 'genre']
database.write(linetowrite)
database.close()

Upvotes: 0

Views: 7875

Answers (1)

pdowling
pdowling

Reputation: 500

You're calling write with a list, the error message is pretty straightforward. To make this work simply (but not necessarily correctly), you could convert the list to a string like so:

database = open('database.csv', 'a')
linetowrite = ['username', 'password', 'forename', 'surname', 'dob', 'artist', 'genre']
database.write(",".join(linetowrite))
database.close()

Better would be to use a proper CSV library, e.g.: https://docs.python.org/3/library/csv.html . Otherwise you're likely to end up with escaping errors (what if one of your fields contains a comma?).

Upvotes: 2

Related Questions