pythonxmlnumpyminidom

numpy.int64' object has no attribute 'replace' while writing xml file using minidom


I've an xml file to be parsed. I did this using minidom parser of python. I had to add an attribute to a particular element which I did after the parsing. Now, I want to write the file back. I am not able to do this.

Below is the error trace which I get for this. I installed numpy 1.8 (win 32 python 2.7) version today. However, I had never tried writing xml file before. Could you please help?

Here's the code snippet:

xmlfile=open(xmlFile,'r')
xmldoc = minidom.parse(xmlFile)
tElements = xmldoc.getElementsByTagName("TEA")
for t in tElements:
    if(t.childNodes):
        print t.nodeType
        dataList = t.childNodes[0].data
        for data, csvData in product(dataList, clusterDataList):
            if(data == csvData[1]):
              t.setAttribute("cluster",csvData[0])
xmlfile.close()
fileWriter=open("sujatha_new.xml",'w')

xmldoc.writexml(fileWriter)
fileWriter.close()

Here's the exception:

Traceback (most recent call last):
  File "C:\Users\w44ylqrl\workspace\Python\Test\T\XMLConverter.py", line 215, in <module>
    addClusterInfo('..\\T\\preprocessed_For_Clustering\\outputs\\sujatha-new.csv', '..\\T\\xml\\sujatha.xml')
  File "C:\Users\w44ylqrl\workspace\Python\Test\T\XMLConverter.py", line 48, in addClusterInfo
    xmldoc.writexml(fileWriter)
  File "C:\Python27\Lib\xml\dom\minidom.py", line 1752, in writexml
    node.writexml(writer, indent, addindent, newl)
  File "C:\Python27\Lib\xml\dom\minidom.py", line 817, in writexml
    node.writexml(writer, indent+addindent, addindent, newl)
  File "C:\Python27\Lib\xml\dom\minidom.py", line 817, in writexml
    node.writexml(writer, indent+addindent, addindent, newl)
  File "C:\Python27\Lib\xml\dom\minidom.py", line 817, in writexml
    node.writexml(writer, indent+addindent, addindent, newl)
  File "C:\Python27\Lib\xml\dom\minidom.py", line 817, in writexml
    node.writexml(writer, indent+addindent, addindent, newl)
  File "C:\Python27\Lib\xml\dom\minidom.py", line 817, in writexml
    node.writexml(writer, indent+addindent, addindent, newl)
  File "C:\Python27\Lib\xml\dom\minidom.py", line 807, in writexml
    _write_data(writer, attrs[a_name].value)
  File "C:\Python27\Lib\xml\dom\minidom.py", line 296, in _write_data
    data = data.replace("&", "&amp;").replace("<", "&lt;"). \
AttributeError: 'numpy.int64' object has no attribute 'replace'

Solution

  • None of the python XML implementations allow you to serialize non-string representations, though some of them will let you assign them, if you want to abuse etree etc. as a treelike data structure. Some (like lxml) won't even let you assign them. Just make it a string:

    t.setAttribute("cluster",str(csvData[0]))
    

    and it will work. If you're deserializing these as well, you'll need to convert values back to int after loading.