TypeError coercing to Unicode with field read from XML file

Randall Parker techiepundit at futurepundit.com
Tue Mar 21 20:41:29 EST 2006


Running Python 2.4.2 on Windows in SPE. I have a very small XML file
(see below) that is in UTF-8 and saved by Windows Notepad as such. I'm
reading it with minidom.

My problem is that once I parse the file with minidom and a field from
it to another variable as shown with this line:
            IPAddr = self.SocketSettingsObj.IPAddress

I get this error:

File "<string>", line 1, in connect
TypeError: coercing to Unicode: need string or buffer, instance found

This is the section of code that reads the XML file:

        ConfigDom = parse(TestSettingsStore.ConfigFileName)
        #ConfigDom
        TSSElList =
ConfigDom.getElementsByTagName("TargetSocketSettings")
        NumTargSocks = len(TSSElList)
        if NumTargSocks > 0:
            TargetIPAddrList =
TSSElList[0].getElementsByTagName("TargetIPAddr")
            TargetIPPortList =
TSSElList[0].getElementsByTagName("TargetIPPort")
            AddrListLen = len(TargetIPAddrList)
            PortListLen = len(TargetIPPortList)
            if AddrListLen > 0 and PortListLen > 0:
                if TargetIPAddrList[0] <> "" and TargetIPPortList[0] <>
0:
                    StillNeedSettings = False

TestSettingsStore.SettingsDictionary['TargetIPAddr'] =
TargetIPAddrList[0]

TestSettingsStore.SettingsDictionary['TargetIPPort'] =
TargetIPPortList[0]

This I saved as Unicode from Notepad with encoding UTF-16:
<?xml version="1.0" encoding="UTF-16" ?>
<TargetSocketSettings>
	<TargetIPAddr>127.0.0.1</TargetIPAddr>
	<TargetIPPort>43210</TargetIPPort>
</TargetSocketSettings>

this I saved as UTF-8 from Notepad with encoding UTF-8:
<?xml version="1.0" encoding="UTF-16" ?>
<TargetSocketSettings>
	<TargetIPAddr>127.0.0.1</TargetIPAddr>
	<TargetIPPort>43210</TargetIPPort>
</TargetSocketSettings>

Both formats create the same error.

I'm probably doing something dumb as I've never done XML in Python
before. Any ideas what?




More information about the Python-list mailing list