1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【python】dbファイルをcsv化する方法

Last updated at Posted at 2019-12-31

pythonでしたほうがいい場合もありますよね。

#方法

  1. splite3でデータベースを読み込む
  2. headerを作り、dataframeを用意する
  3. 各データをdataframeに挿入しcsvとして保存する

以上の要領です。

sqlite3でデータベースを読み込む
import splite3
path = 'path_to_db'
conn = sqlite3.connect(path)
headerを作り、dataframeを用意する
import pandas as pd
header = []
c = conn.cursor()
for column in c.execute('PRAGMA table_info("table_name")'):
    header.append(column[1])
各データをdataframeに挿入し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')

データをappendする度にdfに代入し直しているのがミソですね。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?