Pregunta

I'm trying to parse XML with ElementTree, but I get this error:

xml.etree.ElementTree.ParseError: encoding specified in XML declaration is incorrect

My file.py:

from suds.client import Client
import xml.etree.ElementTree as ET

url = 'http://www.webservicex.com/globalweather.asmx?WSDL'
client = Client(url)
weather = client.service.GetWeather('Sao Paulo', 'Brazil')
print weather

parseWeather = ET.fromstring(weather) # >>>> Here I got my problem! 

When I try to parse my xml from string weather. Anyone know how to solve this kind of problem?

¿Fue útil?

Solución

The weather response is not a string:

>>> type(weather)
<class 'suds.sax.text.Text'>

but ElementTree will turn it into text. The claimed encoding is UTF16 however:

>>> weather.splitlines()[0]
'<?xml version="1.0" encoding="utf-16"?>'

Turn this response into text by explicitly encoding it to UTF-16:

>>> weather = weather.encode('utf16')
>>> parseWeather = ET.fromstring(weather)

Otros consejos

While you can't be sure of the encoding a file should be, I tried changing the xml encoding declaration to utf-8 and ElementTree was able to parse it.

weather = client.service.GetWeather('Sao Paulo', 'Brazil')
weather = weather.replace('encoding="utf-16"?', 'encoding="utf-8"?')
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top