什么应该归我?

时间:2014-02-06 17:35:36

标签: python python-2.7

我使用Python来解析来自SOAP Web服务的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 

3 个答案:

答案 0 :(得分:4)

我会把它写成一个生成器函数,产生dicts,其中键与findtext参数匹配,例如:

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

答案 1 :(得分:0)

您要么返回list dict s:

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个客户实例。

答案 2 :(得分:0)

我会使用字典词典:

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对象的类。