postgresql的copy匯入資料相當于insert操作,之前必須執行create table xxx(......)
實際業務中一個檔案里會包含欄位+資料,但是缺少一步到位的匯入工具
推薦一個針對csv\xlsx的批量匯入工具:https://github.com/zhengze/file_to_db.git
如果檔案第一行是table的欄位名,自動建表+匯入資料,實在是省事,
具體用法請看README:
load data from csv/xlsx into database(mysql/postgresql)
INSTALL
pipenv install #或者pip install -r requirements.txt
CONFIG
- $sudo touch .env
DATABASE_URI=postgresql+psycopg2://admin:1234@localhost/test
USAGE
python file_to_db.py -h
usage: file_to_db.py [-h] [--file FILE] [--table TABLE]
optional arguments:
-h, --help show this help message and exit
--file FILE, -f FILE the file path.
--table TABLE, -t TABLE
the table name.
示例:
python file_to_db.py -f user -t ./test.csv
test.csv內容如下:
name,age zhangsan,15
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/161033.html
標籤:Python
上一篇:Python第十三章-網路編程
