Can I automatically create a table in PostgreSQL from a csv file with headers?

前端 未结 8 754
一整个雨季
一整个雨季 2020-11-30 00:22

I\'m running PostgreSQL 9.2.6 on OS X 10.6.8. I would like to import data from a CSV file with column headers into a database. I can do this with the COPY state

8条回答
  •  忘掉有多难
    2020-11-30 00:55

    I achieved it with this steps:

    1. Convert the csv file to utf8
        iconv -f ISO-8859-1 -t UTF-8 file.txt -o file.csv
    
    1. Use this python script to create the sql to create table and copy
    #!/usr/bin/env python3
    import csv, os
    #pip install python-slugify
    from slugify import slugify
    
    origem = 'file.csv'
    destino = 'file.sql'
    arquivo = os.path.abspath(origem)
    
    d = open(destino,'w')
    with open(origem,'r') as f:
    
        header = f.readline().split(';')
        head_cells = []
        for cell in header:
            value = slugify(cell,separator="_")
            if value in head_cells:
                value = value+'_2'
            head_cells.append(value)
        #cabecalho = "{}\n".format(';'.join(campos))
    
        #print(cabecalho)
        fields= []
        for cell in head_cells:
            fields.append(" {} text".format(cell))
        table = origem.split('.')[0]
        sql = "create table {} ( \n {} \n);".format(origem.split('.')[0],",\n".join(fields))
        sql += "\n COPY {} FROM '{}' DELIMITER ';' CSV HEADER;".format(table,arquivo)
    
        print(sql)
        d.write(sql)
    
    

    3.Run the script with

    python3 importar.py
    

    Optional: Edit the sql script to adjust the field types (all are text by default)

    1. Run the sql script. Short for console
    sudo -H -u postgres bash -c "psql mydatabase < file.sql" 
    

提交回复
热议问题