文件(csv/xlsx)中的數據導入到數據庫中


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

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM