[Python] How to convert db file to csv

Sometimes it's better to use python.

Method

  1. Read the database with splite3
  2. Create header and prepare dataframe
  3. Insert each data into dataframe and save as csv

That's all.

Read database with sqlite3
import splite3
path = 'path_to_db'
conn = sqlite3.connect(path)
Create header and prepare dataframe
import pandas as pd
header = []
c = conn.cursor()
for column in c.execute('PRAGMA table_info("table_name")'):
    header.append(column[1])
Insert each data into dataframe and save as csv
df = pandas.DataFrame(columns=header)
for raw in c.execute('SELECT * FROM table_name ORDER BY index_name):
    series = pd.Series(list(raw), index=df.columns)
    df = df.append(s, ignore_index=True)
df.to_csv('path_to_csv')

It is a miso that the data is reassigned to df every time it append`s.

Recommended Posts

[Python] How to convert db file to csv
How to convert JSON file to CSV file with Python Pandas
How to convert Python to an exe file
[Python] Convert csv file delimiters to tab delimiters
How to read a CSV file with Python 2/3
How to convert Json file to CSV format or EXCEL format
[Python] Write to csv file with Python
Output to csv file with Python
[Python] How to convert a 2D list to a 1D list
How to convert csv to tsv in CLI
Convert psd file to png in Python
[Python] Convert CSV file uploaded to S3 to JSON file with AWS Lambda
After calling the Shell file on Python, convert CSV to Parquet.
[Python] How to store a csv file as one-dimensional array data
How to install Python
How to install python
Read Python csv file
[Python] Convert from DICOM to PNG or CSV
Import Excel file from Python (register to DB)
How to create a JSON file in Python
[Python] How to read excel file with pandas
Convert svg file to png / ico with Python
Writing logs to CSV file (Python, C language)
How to convert SVG to PDF and PNG [Python]
[Python] How to read a csv file (read_csv method of pandas module)
Read CSV file with Python and convert it to DataFrame as it is
[2020.8 latest] How to install Python
How to convert / restore a string with [] in python
[python] Convert date to string
How to install Python [Windows]
Convert numpy int64 to python int
How to convert Python # type for Python super beginners: str
python3: How to use bottle (2)
[Python] Convert list to Pandas [Pandas]
Convert HTML to text file
[Python] How to use list 1
I tried to touch the CSV file with Python
How to update Python Tkinter to 8.6
[Python] Convert PDF text to CSV page by page (2/24 postscript)
How to measure mp3 file playback time with python
How to convert floating point numbers to binary numbers in Python
How to use Python argparse
Convert Scratch project to Python
[Python] Convert Shift_JIS to UTF-8
Python: How to use pydub
How to convert a mel spectrogram back to a wav file
[Python] How to use checkio
How to run Notepad ++ Python
Write to csv with Python
How to save a table scraped by python to csv
[Work efficiency] How to change file names in Python
Convert SDF to CSV quickly
How to change Python version
python, php, ruby How to convert decimal numbers to n-ary numbers
Python script to create a JSON file from a CSV file
How to develop in Python
[python] How to judge scalar
[Python] How to use input ()
How to read csv containing only integers in Python
Convert genbank file to gff file
Download csv file with python