我的回报应该是什么?

2024-03-28 20:14:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Python解析soapweb服务的XML响应。Customer返回大约40个值,如下所示。我想知道是否有一种方法可以让我只需要在我的return语句中输入一个东西,然后得到所有返回的值?我试着使用for customer in doc.findall('.//Customer').itervalues(),但没有成功,因为我相信调用的是词典。同样的结果和推理在.iteritems背后。你知道吗

 doc = ET.fromstring(response_xml)
for customer in doc.findall('.//Customer'):
    customer_number = customer.findtext('CustomerNumber')
    customer_first_name = customer.findtext('FirstName')
    customer_last_name = customer.findtext('LastName')
    customer_middle_name = customer.findtext('MiddleName')
    customer_salutation = customer.findtext('Salutation')
    customer_gender = customer.findtext('Gender')
    customer_language = customer.findtext('Language')
    customer_address1 = customer.findtext('Address1')
    customer_address2 = customer.findtext('Address2')
    customer_address3 = customer.findtext('Address3')
    customer_city = customer.findtext('City')
    customer_county = customer.findtext('County')
    customer_state_code = customer.findtext('StateCode')
    customer_zip_code = customer.findtext('ZipCode')
    customer_phone_number = customer.findtext('PhoneNumber')
    customer_business_phone = customer.findtext('BusinessPhone')
    customer_business_ext = customer.findtext('BusinessExt')
    customer_fax_number = customer.findtext('FaxNumber')
    customer_birth_date = customer.findtext('BirthDate')
    customer_drivers_license = customer.findtext('DriversLicense')
    customer_contact = customer.findtext('Contact')
    customer_preferred_contact = customer.findtext('PreferredContact')
    customer_mail_code = customer.findtext('MailCode')
    customer_tax_exempt_Number = customer.findtext('TaxExmptNumber')
    customer_assigned_salesperson = customer.findtext('AssignedSalesperson')
    customer_type = customer.findtext('CustomerType')
    customer_preferred_phone = customer.findtext('PreferredPhone')
    customer_cell_phone = customer.findtext('CellPhone')
    customer_page_phone = customer.findtext('PagePhone')
    customer_other_phone = customer.findtext('OtherPhone')
    customer_other_phone_desc = customer.findtext('OtherPhoneDesc')
    customer_email1 = customer.findtext('Email1')
    customer_email2 = customer.findtext('Email2')
    customer_optional_field = customer.findtext('OptionalField')
    customer_allow_contact_postal = customer.findtext('AllowContactByPostal')
    customer_allow_contact_phone = customer.findtext('AllowContactByPhone')
    customer_allow_contact_email = customer.findtext('AllowContactByEmail')
    customer_business_phone_ext = customer.findtext('BusinessPhoneExtension')
    customer_internatinol_bus_phone = customer.findtext('InternationalBusinessPhone')
    customer_international_cell = customer.findtext('InternationalCellPhone')
    customer_external_x_reference_key = customer.findtext('ExternalCrossReferenceKey')
    customer_international_fax = customer.findtext('InternationalFaxNumber')
    customer_international_other_phone = customer.findtext('InternationalOtherPhone')
    customer_international_home_phone = customer.findtext('InternationalHomePhone')
    customer_preferred_name = customer.findtext('CustomerPreferredName')
    customer_international_pager = customer.findtext('InternationalPagerPhone')
    customer_preferred_lang = customer.findtext('PreferredLanguage')
    customer_last_change_date = customer.findtext('LastChangeDate')
    customer_vehicles = customer.findtext('Vehicles')
    customer_ccid = customer.findtext('CCID')
    customer_cccd = customer.findtext('CCCD')


webservice.close()
return 

Tags: namenumberdocreturncontactphonecodecustomer
3条回答

我会用字典:

doc = ET.fromstring(response_xml)
customers = {}
cust_dict = {}
for customer in doc.findall('.//Customer'):
    cust_dict['customer_number'] = customer.findtext('CustomerNumber')
    cust_dict['customer_first_name'] = customer.findtext('FirstName')
    cust_dict['customer_last_name'] = customer.findtext('LastName')
    snip snip...
    customers[customer_number] = cust_dict  # or whatever property you want to use to identify each customer, I'm assuming customer_number is some sort of id number        

webservice.close()
return  customers

也就是说,如果没有可以用来创建Customer对象的类。你知道吗

您可以返回listdict

customers = []
for customer in doc.findall('.//Customer'):
    customer_dict = {}
    customer_dict['number']     = customer.findtext('CustomerNumber')
    customer_dict['first_name'] = customer.findtext('FirstName')
    customer_dict['last_name']  = customer.findtext('LastName')
    # ad nauseum
    customers.append(customer_dict)

webservice.close()
return customers

或者创建一个Customer类来处理这个问题,然后返回list个客户实例。你知道吗

我会把它写成一个生成函数,生成dict,其中key与findtext参数匹配,例如:

fields = ['CustomerNumber', 'FirstName', 'LastName',
          # ...
         ]
for customer in doc.findall('.//Customer'):
    yield dict((f, customer.findtext(f)) for f in fields)

相关问题 更多 >