1

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'

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

the data is being added to the right side of previous cell, is there any way to avoid that? writerow is writing whole data into single row
I've updated it to use writerows, missed the fact you're using fetchall, take a crack at it now
how can i add column names while creating csv file
have a single c.writerow(listOfColumnNames) before you call c.writerows(result)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.