文件(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