分享

文件(csv/xslx)中的数据导入到数据库中

 python_lover 2020-05-19

*.sql文件导入数据库通常比较方便。

其他格式如csv、xsl等文件导入比较费劲。

推荐一个针对csv\xsl的导入工具: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

 



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多