I am trying to read a gzip file which contains xml and unicode, but I'm getting an error. The code I am using is:
import gzip
import xml
path = "index.mjml.gz"
gzFile = gzip.open(path, mode='r')
gzContents = gzFile.read()
gzFile.close()
unicodeContents = gzContents.encode('utf-8')
xmlContent = xml.dom.minidom.parseString(unicodeContents)
# Do stuff with xmlContent
When I run this code I get the following error (fails on the line that starts with xmlContent
)
/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/xml/dom/minidom.pyc in parseString(string, parser)
1922 if parser is None:
1923 from xml.dom import expatbuilder
-> 1924 return expatbuilder.parseString(string)
1925 else:
1926 from xml.dom import pulldom
/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/xml/dom/expatbuilder.pyc in parseString(string, namespaces)
938 else:
939 builder = ExpatBuilder()
--> 940 return builder.parseString(string)
941
942
/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/xml/dom/expatbuilder.pyc in parseString(self, string)
221 parser = self.getParser()
222 try:
--> 223 parser.Parse(string, True)
224 self._setup_subset(string)
225 except ParseEscape:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 1141336: ordinal not in range(128)
I found a previous answer similar to this Reading utf-8 characters from a gzip file in python, but I'm still getting an error.
Is there a problem with the xml parser?
(I'm using Python 2.7.?)