You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python新手求助:如何从DespatchBay的AddressType对象提取属性值

问题描述

我是Python新手,正在边学边用DespatchBay的SDK处理寄件工作。调用API获取地址时,返回了AddressType类型的对象,打印出来的内容看起来像字典,但不知道怎么访问里面的属性值。

执行这段代码:

postcode = "NW1 4RY"
street_num = 1
recipient_address = client.find_address(postcode, street_num)
print(recipient_address)

得到输出:

(AddressType){
   CompanyName = "London Zoo"
   Street = "Regents Park"
   Locality = None
   TownCity = "London"
   County = None
   PostalCode = "NW1 4RY"
   CountryCode = "GB"
 }

我不清楚开头的(AddressType)是什么,也不知道怎么提取里面的信息。附上完整代码:

import os
from despatchbay.despatchbay_sdk import DespatchBaySDK
from pprint import pprint

api_user = os.getenv('DESPATCH_API_USER')
api_key = os.getenv('DESPATCH_API_KEY')
client = DespatchBaySDK(api_user=api_user, api_key=api_key)
sender_id = '5536'


# inputs
postcode = "NW1 4RY"
street_num = 1
customer = "Testy Mctestson"
phone = "07666666666"
email = "testicles@tested.com"
num_boxes = 2
collection_date = '2022-09-11'

recipient_address = client.find_address(postcode, street_num)

recipient = client.recipient(
    name=customer,
    telephone=phone,
    email=email,
    recipient_address=recipient_address
)

print (recipient_address)

parcels = []
parcel_names = []
for x in range(num_boxes):
    parcelname = "my_parcel_" + str(x + 1)
    parcel_names.append(parcelname)

for my_parcel in parcel_names:
    go_parcel = client.parcel(
        contents="Radios",
        value=500,
        weight=6,
        length=60,
        width=40,
        height=40,
    )
    parcels.append(go_parcel)

sender = client.sender(
    address_id=sender_id
)

shipment_request = client.shipment_request(
    parcels=parcels,
    client_reference=customer,
    collection_date=collection_date,
    sender_address=sender,
    recipient_address=recipient,
    follow_shipment='true'
)

services = client.get_available_services(shipment_request)
shipment_request.service_id = services[0].service_id
dates = client.get_available_collection_dates(sender, services[0].courier.courier_id)
print(customer + "'s shipment of",num_boxes, "parcels will be collected from: ",recipient['RecipientAddress'], "on", dates[0])
shipment_request.collection_date = dates[0]
added_shipment = client.add_shipment(shipment_request)
client.book_shipments([added_shipment])
shipment_return = client.get_shipment(added_shipment)
label_pdf = client.get_labels(shipment_return.shipment_document_id)
label_pdf.download('./' + customer + '.pdf')
解决方案

1. 直接通过属性名访问

AddressType是SDK定义的自定义类对象,不是字典,所以不能用recipient_address['CompanyName']这种方式访问,而是用**点号(.)**直接调用属性:

# 提取单个属性
print(recipient_address.CompanyName)  # 输出: London Zoo
print(recipient_address.Street)       # 输出: Regents Park
print(recipient_address.PostalCode)   # 输出: NW1 4RY

2. 转换成字典(方便批量查看)

如果想一次性查看所有属性和值,可以把对象转换成字典。大部分SOAP SDK生成的对象都支持用vars()函数转换:

address_dict = vars(recipient_address)
print(address_dict)
# 输出会是类似字典的结构:
# {'CompanyName': 'London Zoo', 'Street': 'Regents Park', ...}

# 然后就可以用字典方式访问
print(address_dict['TownCity'])  # 输出: London

3. 修正你代码中的错误

看你完整代码里的这一行:

print(customer + "'s shipment of",num_boxes, "parcels will be collected from: ",recipient['RecipientAddress'], "on", dates[0])

这里recipient也是SDK生成的对象,不能用recipient['RecipientAddress'],要改成点号访问,还可以优化成更清晰的格式化输出:

print(f"{customer}'s shipment of {num_boxes} parcels will be collected from: {recipient.recipient_address.TownCity}, {recipient.recipient_address.PostalCode} on {dates[0]}")

补充说明

开头的(AddressType)只是对象的类型标识,说明这个实例是AddressType类的对象,不用管它,直接访问属性即可。

内容的提问来源于stack exchange,提问作者prosody

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 00:10:26