2014-02-06 51 views
-1

我正在使用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 

回答

4

我會寫,作爲一個發電機功能產生類型的字典其中關鍵的findtext參數匹配,如:

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

顯然,我不能做到這一點的解決方案2.7 :( – DarthOpto

+0

爲什麼不能有什麼錯誤 –

+0

'? Python版本<3.3不允許在生成器中使用參數'返回' – DarthOpto

0

要麼你想回到dictlist一個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

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對象。