gpt4 book ai didi

Python UPS XML 跟踪

转载 作者:太空宇宙 更新时间:2023-11-04 02:40:45 30 4
gpt4 key购买 nike

所以我基本上是在尝试让 python 输出发货状态。遗憾的是,ClassicUPS 包裹仅在递送时显示日期,在运输途中显示“True”或“False”。

这是默认的“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”文件:

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"

“ups_test.py”的输出:

[{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)']

最佳答案

解决了这个问题。

        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)

关于Python UPS XML 跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46614788/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com