Reputation: 21
I am trying to write a xml file from python (2.7) using xml.append function.
I have a string "Frédéric" that needs to be written to xml file as one of the values. I am trying to use unicode function on this string and then encode function to write to the file.
a ="Frédéric"
unicode(a, 'utf8')
By I get error message as 'ascii' codec can't decode byte 0xe9 in position 9'
I have gone through other stackoverflow posts for this scenario, the suggestion was to add unicode-literal before the string.
a = u'Frédéric'
a.encode('utf8')
Since, my 'a' variable is going to be dynamic (it can take any value from a list) I need to use unicode function.
Any suggestions please?
Thanks
Upvotes: 0
Views: 144
Reputation: 224
Maybe following helps. You can use codecs to save the XML string while using utf-8.
import codecs
def save_xml_string(path, xml_string):
"""
Writes the given string to the file associated with the given path.
:param path:
Path to the file to write to.
:param xml_string:
The string to be written
:return:
nothing
"""
output_file = codecs.open(path, "w", "utf-8")
output_file.write(xml_string)
output_file.close()
Upvotes: 1