簡體   English   中英

使用django將CSV文件導入postgres

[英]import CSV file into postgres using django

我正在嘗試使用Django將csv文件導入postgres DB。

我嘗試以下功能:

import os
from django.db import models
import psycopg2
from postgres_copy import CopyMapping

host = 'localhost'
port = '5432'
dbname = 'sellerhub'
username = 'postgres'
password = 'postgres'


class Reports:
    def __init__(self):
        global host, port, dbname, username, password
        try:
            self.db_conn = psycopg2.connect("host=%s port=%s dbname=%s user=%s password=%s" %(host, port, dbname, username, password))
        except psycopg2.OperationalError:
            print "Database Not Found Or the Credentials are wrong."
        self.cur = self.db_conn.cursor()
def saveUploadedInventory(self, inventory_file):
        #print "Inventory File"
        with open('uploaded_inventory_sheet.csv','wb+') as destination:
            for chunk in inventory_file.chunks():
                destination.write(chunk)
        #print "Inventory Saved."
        copy_sql = """copy fk_invent_temp from stdin WITH CSV HEADER DELIMITER as ',' """
        #print "query created"
        with open('uploaded_inventory_sheet.csv','r') as pmt_file:
            self.cur.copy_expert(sql=copy_sql, file=pmt_file)
        #print "file uploades"
        os.system('rm uploaded_inventory_sheet.csv')
        #print "removes file"

setting.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'sellerhub',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 'localhost',
        'PORT': '5432',
        }
    }

該函數完全執行,沒有錯誤,

但是fk_invent表中沒有數據。

如果我直接使用成功上傳的PGAdmin3 UI導入該文件。 請問有誰能告訴我我做錯了嗎?

我通過逐行插入獲得臨時解決方案:

with open('uploaded_inventory_sheet.csv','wb+') as destination:
            for chunk in inventory_file.chunks():
                destination.write(chunk)
        print "Inventory Saved."
        reader = csv.reader(open('uploaded_inventory_sheet.csv','rb'))
        count = 0 
        for row in reader:
            if count == 0:
                count=1
                continue
            # print row[0],"\n"
            count = count +1
            try:
                self.cur.execute("""INSERT into  fk_payment_temp values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""",(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8],row[9],row[10],row[11],row[12],row[13],row[14],row[15],row[16],row[17],row[18],row[19],row[20],row[21],row[22],row[23],row[24],row[25],row[26],row[27],row[28],row[29],row[30],row[31],row[32],row[33],row[34],row[35],row[36],row[37],row[38],row[39],row[40],row[41],row[42],row[43],row[44]))
                print "INSERT into  fk_payment_temp values('",row[0],"','",row[1],"''",row[2],"','",row[3],"''",row[4],"','",row[5],"''",row[6],"','",row[7],"''",row[8],"','",row[9],"''",row[10],"','",row[11],"''",row[12],"','",row[13],"''",row[14],"','",row[15],"''",row[16],"','",row[17],"''",row[18],"','",row[19],"''",row[20],"','",row[21],"''",row[22],"','",row[23],"''",row[24],"','",row[25],"''",row[26],"','",row[27],"''",row[28],"','",row[29],"''",row[30],"','",row[31],"''",row[32],"','",row[33],"''",row[34],"','",row[35],"''",row[36],"','",row[37],"''",row[38],"','",row[39],"''",row[40],"','",row[41],"''",row[42],"','",row[43],"''",row[44],"','",row[45],"')"
            except:
                pass
        print count
        self.cur.callproc("flip_payment_test")
        self.cur
        self.db_conn.commit()

        print "file uploades"
        os.system('rm uploaded_inventory_sheet.csv')
        print "removes file" 

最簡單,最干凈的解決方案是使用Django manage.py命令導入json夾具文件。 因此,我建議您將CSV文件轉換為json並使用manage.py loaddata命令將其加載到數據庫中:

python manage.py loaddata yourfile.json

您可以在整個Internet上找到CSV到json轉換器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM