copy data from csv to postgresql using python

前端 未结 8 1173
遥遥无期
遥遥无期 2021-02-04 03:32

I am on windows 7 64 bit. I have a csv file \'data.csv\'. I want to import data to a postgresql table \'temp_unicommerce_status\' via a python script.

My Script is:

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-04 03:44

    You can use d6tstack which makes this simple

    import d6tstack
    import glob
    
    c = d6tstack.combine_csv.CombinerCSV([r'C:\Users\n\Desktop\data.csv']) # single-file
    c = d6tstack.combine_csv.CombinerCSV(glob.glob('*.csv')) # multi-file
    c.to_psql_combine('postgresql+psycopg2://psqlusr:psqlpwdpsqlpwd@localhost/psqltest', 'tablename')
    

    It also deals with data schema changes, create/append/replace table and allows you to preprocess data with pandas.

提交回复
热议问题