2017-10-06 90 views
1

所以我基本上試圖讓python輸出貨件的狀態。不幸的是,ClassicUPS軟件包只會顯示交付的日期,以及運送中的「真」或「假」IF。Python UPS XML Tracking

這是默認的 「ups.py」:

class TrackingInfo(object): 

def __init__(self, ups_conn, tracking_number): 
    self.tracking_number = tracking_number 

    tracking_request = { 
     'TrackRequest': { 
      'Request': { 
       'TransactionReference': { 
        'CustomerContext': 'Get tracking status', 
        'XpciVersion': '1.0', 
       }, 
       'RequestAction': 'Track', 
       'RequestOption': 'activity', 
      }, 
      'TrackingNumber': tracking_number, 
     }, 
    } 

    self.result = ups_conn._transmit_request('track', tracking_request) 

@property 
def shipment_activities(self): 
    # Possible Status.StatusType.Code values: 
    # I: In Transit 
    # D: Delivered 
    # X: Exception 
    # P: Pickup 
    # M: Manifest 

    shipment_activities = (self.result.dict_response['TrackResponse'] 
            ['Shipment']['Package']['Activity']) 
    if type(shipment_activities) != list: 
     shipment_activities = [shipment_activities] 

    return shipment_activities 
@property 
def ship_status(self): 
    # Possible Status.StatusType.Code values: 
    # I: In Transit 
    # D: Delivered 
    # X: Exception 
    # P: Pickup 
    # M: Manifest 

    ship_status = (self.result.dict_response['TrackResponse']['Shipment']['Package']['Activity']['Status']['StatusType']['Description']) 
    if type(ship_status) == list: 
     ship_status = [ship_status] 

    return ship_status 
@property 
def delivered(self): 
    delivered = [x for x in self.shipment_activities 
       if x['Status']['StatusType']['Code'] == 'D'] 
    if delivered: 
     return datetime.strptime(delivered[0]['Date'], '%Y%m%d') 

@property 
def in_transit(self): 
    in_transit = [x for x in self.shipment_activities 
       if x['Status']['StatusType']['Description'] != ''] 

    return len(in_transit) > 0 ### If I remove "len" and "> 0" then it shows a string of results which I will show below 

我自己創作的 「ups_test.py」 文件:

:從 「ups_test.py」

from ClassicUPS import UPSConnection 
from datetime import datetime 
import xmltodict 
import xml.etree.ElementTree as ET 

# Credentials obtained from the UPS website 
ups = UPSConnection('api-key', 
       'username', 
       'password', 
       'shipper#', # Optional if you are not creating a shipment 
       debug=False)  # Use the UPS sandbox API rather than prod 

tracking = ups.tracking_info('1ZXXXXXXXXXXXXXXXX') 



print tracking.delivered 
print tracking.ship_status 
# try: 
# if tracking.in_transit == (False) : 
    # print "Still in Transit" 
# elif tracking.in_transit == (True) : 
    # print ("Delivered") 
# except: 
    # print "Error" 

try: 
    if tracking.delivered == (None): 
     print ("Still in Transit") 
    elif tracking.delivered != (None): 
     Print ("Delivered") 
except: 
    print "error" 

輸出

[{u'Status': {u'StatusType': {u'Code': u'I', u'Description': u'THE SHIPMENT HAS 
BEEN DROPPED OFF AND IS NOW AT THE UPS STORE(R)'}, u'StatusCode': {u'Code': u'XD 
'}}, u'Date': u'20171006', u'ActivityLocation': {u'Address': {u'City': u'INDIANA 
POLIS', u'StateProvinceCode': u'IN', u'CountryCode': u'US'}}, u'Time': u'180000' 
}, {u'Status': {u'StatusType': {u'Code': u'M', u'Description': u'BILLING INFORMA 
TION RECEIVED'}, u'StatusCode': {u'Code': u'MP'}}, u'Date': u'20171005', u'Activ 
ityLocation': {u'Address': {u'CountryCode': u'US'}}, u'Time': u'153442'}] 

我的問題是,我怎麼能得到python輸出具體的數據,如:

Description = u['Description' : u'THE SHIPMENT HAS 
BEEN DROPPED OFF AND IS NOW AT THE UPS STORE(R)'] 
+0

您需要了解名爲'tracking'的對象的結構。它的成員是什麼?它們的類型是什麼?一旦你知道'tracking'中的信息是如何組織的,你需要決定你想要的數據並寫入邏輯來訪問它。沒有普遍的答案。 –

+0

我在尋找幫助的答案是如何從輸出中解析該字符串。當我需要幫助的時候,我不知道爲什麼你會問跟蹤對象是如何解析輸出。 –

+0

因爲常規方法是瞭解對象的結構,而不是將它們轉換爲字符串,然後解析字符串。這通常更困難,並且仍然給你帶來幾乎相同的問題:除非你理解它的結構和語法,否則你不能可靠地解析字符串。我也被一個事實所誤導,那就是你並沒有在任何地方說出你想要解析字符串的幫助。對不起,如果我浪費你的時間。 –

回答

0

解決了這個問題。

 tracking = ups.tracking_info(CusTracking) 
     status = tracking.shipment_activities 

     if status[0]['Status']['StatusType']['Code'] == 'M': 
      print "Ready for pickup" 
      cursor.execute("UPDATE rma_order SET from_cus_tracking_status=1 WHERE order_id=%s" % Id)