How to export sqlite to CSV in Python without being formatted as a list?

后端 未结 3 1360
别那么骄傲
别那么骄傲 2020-12-05 10:41

Here is what I currently have:

conn = sqlite3.connect(dbfile)
conn.text_factory = str ## my current (failed) attempt to resolve this
cur = conn.cursor()
data         


        
相关标签:
3条回答
  • 2020-12-05 11:16

    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)
    
    0 讨论(0)
  • 2020-12-05 11:20

    my version that works without issues with just a couple of lines.

        import pandas as pd
    
        conn = sqlite3.connect(db_file, isolation_level=None,
                               detect_types=sqlite3.PARSE_COLNAMES)
        db_df = pd.read_sql_query("SELECT * FROM error_log", conn)
        db_df.to_csv('database.csv', index=False)
    

    If you want a table separated file, change the .csv extension to .tsv and add this sep='\t'

    0 讨论(0)
  • 2020-12-05 11:25

    Converting an sqlite database table to csv file can also be done directly using sqlite3 tools:

    >sqlite3 c:/sqlite/chinook.db
    sqlite> .headers on
    sqlite> .mode csv
    sqlite> .output data.csv
    sqlite> SELECT customerid,
       ...>        firstname,
       ...>        lastname,
       ...>        company
       ...>   FROM customers;
    sqlite> .quit
    

    The above sqlite3 commands will will create a csv file called data.csv in your current directory (of course this file can be named whatever you choose). More details are available here: http://www.sqlitetutorial.net/sqlite-export-csv/

    0 讨论(0)
提交回复
热议问题