简体   繁体   中英

Export SQLite table to csv file with double quotes in python

I am trying to export a SQLite table which includes double quotes for string data type using python.

Below is my code:

import sqlite3
import pandas
import csv
import re
    
def connect():
    conn=sqlite3.connect("lite.db")
    cur=conn.cursor()
    cur.execute("CREATE TABLE IF NOT EXISTS calculation (ID INTEGER PRIMARY KEY,Station_ID INTEGER,Virtual_Variable TEXT,\
                    Variable_ID INTEGER,Unit_ID INTEGER,Formula TEXT,RoC_Active INTEGER,RoC_Precision INTEGER,\
                    RoC_Period_Value INTEGER,RoC_Period_Type INTEGER,RoC_Unit_Value INTEGER,RoC_Unit_Type INTEGER,\
                    Datum_Variable_ID INTEGER,Datum_Timestamp TEXT,Datum_Information TEXT,Constants TEXT)")
    conn.commit()
    conn.close()

    
def insert(Station_ID,Virtual_Variable,Variable_ID,Unit_ID,Formula):
    conn=sqlite3.connect("lite.db")
    cur=conn.cursor()
    cur.execute("INSERT INTO calculation VALUES(NULL,?,?,?,?,?,0,0,1,3600,1,3600,0,\"0000-00-00 00:00:00\",0,\"\")",(Station_ID,Virtual_Variable,Variable_ID,Unit_ID,Formula))
    conn.commit()
    conn.close()
    
def export():
    conn = sqlite3.connect('lite.db')
    cur=conn.cursor()
    cur.execute("SELECT * FROM calculation")
    rows=cur.fetchall()
    csv_path = "output.csv"
    with open(csv_path, "w", newline="") as csv_file:
        csv_writer = csv.writer(csv_file,delimiter=";")
        # Write headers.
        csv_writer.writerow([i[0] for i in cur.description])
        # Write data.
        csv_writer.writerows(rows)

The insert function yields a list of tuples:

[(1, 1669, '93007AX0026_01_M_Tass_Glo', '', 42, '(Z145+Z146+Z147+Z148+Z149+Z150)/6', 0, 0, 1, 3600, 1, 3600, 0, '0000-00-00 00:00:00', '0', '')]

When i call the export function, it would give the output as:

ID;Station_ID;Virtual_Variable;Variable_ID;Unit_ID;Formula;RoC_Active;RoC_Precision;RoC_Period_Value;RoC_Period_Type;RoC_Unit_Value;RoC_Unit_Type;Datum_Variable_ID;Datum_Timestamp;Datum_Information;Constants
1;1669;93007AX0026_01_M_Tass_Glo;;42;(Z145+Z146+Z147+Z148+Z149+Z150)/6;0;0;1;3600;1;3600;0;0000-00-00 00:00:00;0;

I attempted to put '"' before and after the argument in insert function:

cur.execute("INSERT INTO calculation VALUES(NULL,?,?,?,?,?,0,0,1,3600,1,3600,0,\"0000-00-00 00:00:00\",0,\"\")",(Station_ID,'"'+Virtual_Variable+'"',Variable_ID,Unit_ID,Formula))

But it returns triple of double quotes:

ID;Station_ID;Virtual_Variable;Variable_ID;Unit_ID;Formula;RoC_Active;RoC_Precision;RoC_Period_Value;RoC_Period_Type;RoC_Unit_Value;RoC_Unit_Type;Datum_Variable_ID;Datum_Timestamp;Datum_Information;Constants
1;1669;"""93007AX0026_01_M_Tass_Glo""";;42;(Z145+Z146+Z147+Z148+Z149+Z150)/6;0;0;1;3600;1;3600;0;0000-00-00 00:00:00;0;

How can I get the desired output as below?

ID;Station_ID;Virtual_Variable;Variable_ID;Unit_ID;Formula;RoC_Active;RoC_Precision;RoC_Period_Value;RoC_Period_Type;RoC_Unit_Value;RoC_Unit_Type;Datum_Variable_ID;Datum_Timestamp;Datum_Information;Constants
1;1669;"93007AX0026_01_M_Tass_Glo";;42;"(Z145+Z146+Z147+Z148+Z149+Z150)/6";0;0;1;3600;1;3600;0;"0000-00-00 00:00:00";"0";""

Noted that there would be no double quotes between "93007AX0026_01_M_Tass_Glo" and 42.

Any suggestion is appreciated.

Change the query:

cur.execute("SELECT * FROM calculation")

to:

sql = """
SELECT
  ID,
  Station_ID,
  '"' || COALESCE(Virtual_Variable, '') || '"' Virtual_Variable,
  Variable_ID,
  Unit_ID,
  '"' || COALESCE(Formula, '') || '"' Formula,
  RoC_Active,
  RoC_Precision,
  RoC_Period_Value,
  RoC_Period_Type,
  RoC_Unit_Value,
  RoC_Unit_Type,
  Datum_Variable_ID,
  '"' || COALESCE(Datum_Timestamp, '') || '"' Datum_Timestamp,
  '"' || COALESCE(Datum_Information, '') || '"' Datum_Information,
  '"' || COALESCE(Constants, '') || '"' Constants
FROM calculation 
"""
cur.execute(sql)

so that all TEXT columns are returned enclosed in double quotes and then export the resultset.

Also change this line:

csv_writer = csv.writer(csv_file,delimiter=";")

with:

csv_writer = csv.writer(csv_file,delimiter=";",quotechar='',quoting=csv.QUOTE_NONE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM