Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3458576
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:59:58+00:00 2026-05-18T09:59:58+00:00

It is possible export sqlite3 table to csv or xls format? I’m using python

  • 0

It is possible export sqlite3 table to csv or xls format? I’m using python 2.7 and sqlite3.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:59:58+00:00Added an answer on May 18, 2026 at 9:59 am

    I knocked this very basic script together using a slightly modified example class from the docs; it simply exports an entire table to a CSV file:

    import sqlite3
    import csv, codecs, cStringIO
    
    class UnicodeWriter:
        """
        A CSV writer which will write rows to CSV file "f", 
        which is encoded in the given encoding.
        """
    
        def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
            # Redirect output to a queue
            self.queue = cStringIO.StringIO()
            self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
            self.stream = f
            self.encoder = codecs.getincrementalencoder(encoding)()
    
        def writerow(self, row):
            self.writer.writerow([unicode(s).encode("utf-8") for s in row])
            # Fetch UTF-8 output from the queue ...
            data = self.queue.getvalue()
            data = data.decode("utf-8")
            # ... and reencode it into the target encoding
            data = self.encoder.encode(data)
            # write to the target stream
            self.stream.write(data)
            # empty queue
            self.queue.truncate(0)
    
        def writerows(self, rows):
            for row in rows:
                self.writerow(row)
    
    conn = sqlite3.connect('yourdb.sqlite')
    
    c = conn.cursor()
    c.execute('select * from yourtable')
    
    writer = UnicodeWriter(open("export.csv", "wb"))
    
    writer.writerows(c)
    

    Hope this helps!

    Edit: If you want headers in the CSV, the quick way is to manually add another row before you write the data from the database, e.g:

    # Select whichever rows you want in whatever order you like
    c.execute('select id, forename, surname, email from contacts')
    
    writer = UnicodeWriter(open("export.csv", "wb"))
    
    # Make sure the list of column headers you pass in are in the same order as your SELECT
    writer.writerow(["ID", "Forename", "Surname", "Email"])
    writer.writerows(c)
    

    Edit 2: To output pipe-separated columns, register a custom CSV dialect and pass that into the writer, like so:

    csv.register_dialect('pipeseparated', delimiter = '|')
    
    writer = UnicodeWriter(open("export.csv", "wb"), dialect='pipeseparated')
    

    Here’s a list of the various formatting parameters you can use with a custom dialect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to Export a MySQL to a CSV using the command line
Is it possible to export a site definition (NOT a site template) using stsadm?
It is possible to export/print Visual Studio test results in some document format, maybe
Is it possible to limit export formats only PDF and Excel ?
Possible Duplicate: How do you send email from a Java app using Gmail? How
Is it possible to export a flash movie with a transparent background as a
One of my teammates has asked if it is possible to export from one
Is it possible to export my maven Java project on Eclipse to users that
Possible Duplicate: Can I export and import Visual Studio 2010-extensions? I want to reinstall
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.