What the error message says is that you cannot write the list to a file, but only a βcharacter buffer objectβ, which means a string or something else that acts like a string.
If you just want to write the list to a file the same way you print it to the console, you can write str(thefile) or repr(thefile) (or even use the redirect syntax in print instead of using file.write ).
But you are using the csv module to read the input and presumably want the result to be in the same format, so you probably want to use csv to write it.
You read like this:
list(csv.reader(open(filename, 'rb'), delimiter=',', quotechar='"'))[1:]
So write like this:
csv.writer(open('foo.csv', 'wb'), delimiter=',', quotechar='"').writerows(thefile)
I should mention that I would not structure the code like this in the first place; I would do something like this:
with open('input.csv', 'rb') as infile, open('output.csv', 'wb') as outfile: incsv = csv.reader(infile, delimiter=',', quotechar='"') outcsv = csv.writer(outfile, delimiter=',', quotechar='"') incsv.read()
abarnert
source share