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

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.address matches the actual field name returned by your SOAP server. If the address is stored under a different key (like fullAddress), 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/except block 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:42:20