Python Postgres - AttributeError:' unicode'对象没有属性'数据'

时间:2017-06-01 06:46:47

标签: python postgresql unicode

我收到的错误是

  

回溯(最近一次呼叫最后一次):文件" main.py",第75行,in          VALUES(%d,%f,%s,%s,%s,%s,%s,%f,%d)",(company_id,data [" FOB_value"],str (数据[" FOB_currency"]),数据[" origin_port_name&#34],数据[" destination_port_name&#34]。数据[" origin_port_code&#34],数据[" destination_port_code&#34],数据[" INVOICE_NUMBER&#34],数据[" shipping_bill_number"]))   AttributeError:' unicode'对象没有属性'数据'

我的代码:

data = {    "products" :
                            {
                              "name": "",
                              "hs_code": "",
                              "unit_price": "",
                              "unit_currency": "",
                              "quantity": "",
                              "total_price": ""
                            }
        }

data["FOB_currency"] = obj["Currency "]
data["origin_port_name"] = "MUNDRA"
data["destination_port_name"] = obj["Port of Discharge"]
data["origin_port_code"] = "INMUN1"
data["destination_port_code"] = get_port_code(obj["Port of Discharge"])
iec = str(int(obj["IEC"]))
if len(iec) == 9 :
    data["IEC"] = "0"+iec
else:
    data["IEC"] = iec

data["products"]["quantity"]  = obj["Quantity "]
data["products"]["unit_price"] = obj["Item rate "]
data["products"]["name"] = obj["Item Desc "]
data["products"]["hs_code"] = int(obj["RITC Code "])
data["products"]["unit_currency"] = obj["Currency "]
data["FOB_value"] = obj["FOB "]
data["shipping_bill_date"] = obj["SB.Date"]
data["shipping_bill_no"] = int(obj["SB.No."])
data["invoice_number"] = obj["Invoice No "]




company_id = None
for e in company_rows:
    if e[1] == data["IEC"]:
        company_id = e[0]

错误在这里 -

if company_id != None :
    cur.execute("INSERT INTO shipping_bills (company_id,fob_value,fob_currency,origin_port_name,destination_port_name,origin_port_code,destination_port_code,invoice_number,shipping_bill_number) \
                 VALUES  (%d,%f,%s,%s,%s,%s,%s,%f,%d)",(company_id,data["FOB_value"],str(data["FOB_currency"]),data["origin_port_name"],data["destination_port_name"].data["origin_port_code"],data["destination_port_code"],data["invoice_number"],data["shipping_bill_number"])) ;

    total_price = data["products"]["unit_price"]*data["products"]["quantity"]
    cur.execute("INSERT INTO shipping_bills_products (shipping_bill_id,total_price,name,unit_price,unit_currency,hss_code,quantity)  \
                VALUES (%s,%f,%s,%f,%s,%d,%f)",(cur.fetchone()[0],total_price,str(data["products"]["name"]),data["products"]["unit_price"],str(data["products"]["unit_currency"]),data["products"]["hs_code"],data["products"]["quantity"])) ;

1 个答案:

答案 0 :(得分:0)

data["destination_port_name"].data["origin_port_code"]

.应为逗号,