Python SOAP响应处理:将返回的---转换为None的方法求助
Fixing the "---" to None Conversion in Your SOAP Client Code
Hey there! I totally get how frustrating it is when you're stuck on a small issue after days of trying—let's get this sorted out for you.
The core problem here is straightforward: whenever the SOAP server returns --- as the address, we need to swap that out for None in your Python code. Here's how to modify your soap.py to handle this:
Modified Code with Conversion Logic
import logging from suds.client import Client logging.getLogger('suds.client').setLevel(logging.DEBUG) from urllib import getproxies def return_address(country_code, vat_number): if country_code is None and vat_number is None: return None # Filled in the common VIES WSDL URL since yours was truncated VIES_URL = "http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl" client = Client(VIES_URL, proxies=getproxies()) try: # Call the SOAP service (adjust method name if your service uses a different one) response = client.service.checkVat(countryCode=country_code, vatNumber=vat_number) # Extract the address from the response (use your actual field name if it's different) raw_address = response.address # Convert "---" to None, also handle accidental whitespace around the value if raw_address.strip() == "---": processed_address = None else: processed_address = raw_address return processed_address except Exception as e: logging.error(f"Failed to retrieve address: {str(e)}") return None
Key Details to Note:
- Field Name Check: Double-check that
response.addressmatches the actual field name returned by your SOAP server. If the address is stored under a different key (likefullAddress), adjust that line to match. - Robustness Upgrade: If you also want to treat empty strings or whitespace-only values as
None, expand the condition to:if not raw_address.strip() or raw_address.strip() == "---": processed_address = None - Error Handling: The
try/exceptblock ensures your function won't crash if the SOAP request fails (network issues, invalid VAT number, etc.), and logs errors to help you debug later.
That should do it! Give this a test and let me know if you run into any other small hurdles.
内容的提问来源于stack exchange,提问作者Linkan
相关产品推荐
相关产品推荐

