Sqlite3 / python - Export from sqlite to csv text file does not exceed 20k
You can try using pandas to load the sql data and then to dump it into a csv. You'd have to install the dependencies (notably NumPy) to use it though. It's really simple then:
import sqlite3
import pandas.io.sql as sql
con = sqlite3.connect('database.db')
table = sql.read_frame('select * from some_table', con)
table.to_csv('output.csv')
Exporting CSV file from python sqlite3
The code below will iterate over a list of tuples that SELECT
(execute
) returns.
for row in cursor.execute("SELECT * FROM table"):
write_file.write(row)
Eg.
If you have a database with rows number
, word
, colour
your code will loop over something like this:
(23, "tree", "red")
(17, "code", "green")
(11, "python", "yellow")
And what you're trying to do is write a tuple to a file. You can only write strings (or bytes
) to a file.
What you have to do is convert that tuple to a string. For this you can use the join()
function.
Your code will now look like this:
c.execute("SELECT * FROM table")
table = c.fetchall()
print(table)
#to export as csv file
with open("wub.csv", "wb") as write_file:
cursor = connection.cursor()
for row in cursor.execute("SELECT * FROM table"):
writeRow = " ".join(row)
write_file.write(writeRow)
" ".join(row)
will join all tuple elements and seperate them with, in this case, a space.
There are a few problems with this.
Join only works with lists and tuples that contain string elements only (so the code above wouldn't work if your database had a REAL
or INTEGER
type)
You can just write a loop that goes over each element in the list and converts it to a string.
I also added a .encode()
because you are opening your CSV file with "rb"
so you need to convert it to bytes
before writing to the file.
c.execute("SELECT * FROM table")
table = c.fetchall()
print(table)
#to export as csv file
with open("wub.csv", "wb") as write_file:
cursor = connection.cursor()
for row in cursor.execute("SELECT * FROM table"):
writeRow = " ".join([str(i) for i in row])
write_file.write(writeRow.encode())
Exporting table from sqlite3
You should be able to dump the table like so:
.output filename
.dump tablename
It'll be dumped to the current folder with the filename you specify.
How to export sqlite to CSV in Python without being formatted as a list?
What you're currently doing is printing out the python string representation of a tuple, i.e. the return value of str(row)
. That includes the quotes and 'u's and parentheses and so on.
Instead, you want the data formatted properly for a CSV file. Well, try the csv
module. It knows how to format things for CSV files, unsurprisingly enough.
with open('output.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerow(['Column 1', 'Column 2', ...])
writer.writerows(data)
Related Topics
Sql 2008 Vs 2012 Error: Incorrect Syntax Near The Keyword 'Compute'
Is Postgresql Order Fully Guaranteed If Sorting on a Non-Unique Attribute
Best Way to Change Clustered Index (Pk) in SQL 2005
Hive Left Semi Join for 'Not Exists'
Sql - Create Database and Tables in One Script
Datareader.Getfieldtype Returned Null
How to Specify an Input SQL File with Bcp
Migrating Oracle Date Columns to Timestamp with Timezone
Oracle Sql: How to Read-And-Increment a Field
Sql Server: Do I Need to Use Go Statements Between Batches
In SQL Server, How to Convert Binary Strings to Binary
MySQL to Get The Count of Rows That Fall on a Date for Each Day of a Month
Difference of Create Index by Using Include Column or Not Using
How to Execute SQL Statements in Command Prompt (Cmd)