0
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.

フォルダ内のCSVファイルを一括でDBに取り込む

Posted at

GTFS-JPを取り込む際、フォルダ内のCSVファイルをすべてDB(PostgreSQL)に取り込む必要性に駆られました。

バイト先の方が、Pandasに取り込んでからDBにそのまま入れられると教えてくださったので、その時のメモです。

├─csv_importer.py
└─GTFS
  ├─file1.csv
  └─file2.csv

csv_importer.py

import pandas as pd
import glob
from sqlalchemy import create_engine

engine = create_engine('DB接続用文字列')
file_list = glob.glob("./GTFS/*")

for filename in file_list:
    print(filename[7:])
    df  = pd.read_csv(filename)
    df.to_sql(name=filename[7:],con=engine,if_exists='replace',index=None)
0
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
0
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?