anonymous_coder
anonymous_coder

Reputation: 139

writing to csv from cursor.fetchall() in python

i am trying to write fetchall result to csv file using writerow, using sqlite3 package of python

import sqlite3
import time,csv
s=time.time()
con = sqlite3.connect("dreamtool.db")
cur = con.cursor()

cur.execute("select * from result3")
result = cur.fetchall()

c = csv.writer(open("temp.csv","wb"))
c.writerow(result)

e=time.time()

con.close()
print(e-s)

i am getting error as a bytes-like object is required, not 'str'

Upvotes: 1

Views: 2125

Answers (1)

M Z
M Z

Reputation: 4799

Use:

c = csv.writer(open("temp.csv","w"))
c.writerows(result)

To have the mode as "wb" means you want to write byte-level information, which is not what you have.

Upvotes: 3

Related Questions