在python django中使用copy_from删除csv头?
我正在使用以下代码:在python django中使用copy_from删除csv头?,python,django,postgresql,csv,Python,Django,Postgresql,Csv,我正在使用以下代码: def saveUploadedInventory(self, inventory_file,user_id): print "Inventory File" with open('uploaded_inventory_sheet.csv','wb+') as destination: for chunk in inventory_file.chunks(): destination.w
def saveUploadedInventory(self, inventory_file,user_id):
print "Inventory File"
with open('uploaded_inventory_sheet.csv','wb+') as destination:
for chunk in inventory_file.chunks():
destination.write(chunk)
print "Inventory Saved."
f = open('uploaded_inventory_sheet.csv','rb')
self.cur.copy_from(f, 'fk_payment_temp', sep=',', columns=('settlement_ref_no', 'order_type', 'fulfilment_type', 'seller_sku',
'wsn', 'order_id', 'order_item_id', 'order_date', 'dispatch_date', 'delivery_date',
'cancellation_date', 'settlement_date', 'order_status', 'quantity', 'order_item_value',
'sale_transaction_amount', 'discount_transaction_amount', 'refund',
'protection_fund', 'total_marketplace_fee', 'service_tax', 'swach_bharat_cess',
'settlement_value', 'commission_rate', 'commission', 'payment_rate',
'payment_fee', 'fee_discount', 'cancellation_fee', 'fixed_fee', 'emi_fee',
'total_weight', 'weight_type', 'shipping_fee', 'reverse_shipping_fee',
'shipping_zone', 'token_of_apology', 'pick_and_pack_fee', 'storage_fee',
'removal_fee', 'invoice_id', 'invoice_date', 'invoice_amount', 'sub_category',
'total_offer_amount', 'my_offer_share', 'flipkart_offer_share'))
它给出了以下错误:
类型时间戳的输入语法无效:“订单日期”
这意味着应该有某个日期,而不是订单日期
这里的订单日期是标题
在postgresql中:
COPY Table_Name FROM 'wheat_crop_data.csv' DELIMITER ',' CSV HEADER;
如何在python django中实现这一点?然后您应该切掉第一行,并将剩余的内容输入光标:
from StringIO import StringIO
with open('uploaded_inventory_sheet.csv') as f:
next(f) # skip the first line
content = StringIO('\n'.join(line for line in f))
self.cur.copy_from(content, ...)
注意:此解决方案将整个文件保存在内存中。如果这不是预期的行为,您可以使用临时中间文件。从复制是您接受的唯一方式,还是通过Django ORM创建数据也可以?我不知道ORM。如果使用copy From的任何解决方案对我来说都是更好的,因为我又遇到了一个错误。要么我在这里告诉你,要么我需要问另一个问题。如果这是一个不同的错误,你应该打开另一个问题,这样那些寻找类似问题的人会更快地找到答案。