How to fix unicode error while generating xml file using python xml.etree.ElementTree without misssing any data?
Question: I am generating xml file using xml.etree.ElementTree in python and then
writting the generated xml file into a file. One of the tag of the xml is
regarding system installed software which will have all the details of the
system installed software. The xml output on console which i get on execution
of the script is perfect but when i try to place output into a file i
encounter following error as:

    
    
    Traceback (most recent call last):
    File "C:\xmltry.py", line 65, in <module>
    f.write(prettify(top))
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in position 4305: ordinal not in range(128)
    

Following is the script:

    
    
    def prettify(elem):
    """Return a pretty-printed XML string for the Element.
    """
    rough_string = ElementTree.tostring(elem, 'utf-8')
    reparsed = minidom.parseString(rough_string)
    return reparsed.toprettyxml(indent="  ")
    
    ##Here starts populating elements inside xml file
    top = Element('my_practice_document')
    comment = Comment('details')
    top.append(comment)
    child = SubElement(top, 'my_information')
    childs = SubElement(child,'my_name')
    childs.text = str(options.my_name)
    
    #Following section is for retrieving list of software installed on the system
    import wmi
    w = wmi.WMI()
    for p in w.Win32_Product():
    if (p.Version is not None) and (p.Caption is not None):
        child = SubElement(top, 'sys_info') 
        child.text =  p.Caption + " version "+ p.Version 
    
    ## Following portion places the xml output into test.xml file
    with open("test.xml", 'w') as f:
        f.write(prettify(top))
    

When the script is executed i get the unicode error . I searched on internet
and tried out following also:

    
    
    import sys
    reload(sys)
    sys.setdefaultencoding('utf8')
    

But this also did not resolved my issue. I want to have all the data which i
am getting on console into the file without missing anything. So, how can i
achieve that. Thanx in advance for your assistance.


Answer: You need to specify an encoding for your output file; `sys.setdefaultencoding`
doesn't do that for you.

Try

    
    
    import codecs
    with codecs.open("test.xml", 'w', encoding='utf-8') as f:
        f.write(prettify(top))
    

