1

I am using following code:

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'))

It is giving following error:

invalid input syntax for type timestamp: "Order Date"

that mean clear there should be some date instead of Order Date

here order date is header.

In postgresql:

COPY Table_Name FROM 'wheat_crop_data.csv' DELIMITER ',' CSV HEADER;

how i can do this in python django ?

2
  • Is the COPY FROM the only way you accept, or creating the data via Django ORM is fine too? Commented Jan 14, 2016 at 9:30
  • I dont know about ORM. It is better for me if any solution with copy from Commented Jan 14, 2016 at 9:34

1 Answer 1

4

Then you should chop off the first line and feed the remainder to the cursor:

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, ...)

Note this solution will hold the entire file in memory. If that is not an expected behavior, you may use the temporary intermediate file.

Sign up to request clarification or add additional context in comments.

3 Comments

I am getting one more error. either i tell you here or i need to ask another question.
If it's a different error, you'd open another question, so that folks who will be looking for similar things will find an answer quicker.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.