我想解析我的XML文档。所以我将我的XML文档存储如下
class XMLdocs(db.Expando): id = db.IntegerProperty() name=db.StringProperty() content=db.BlobProperty()
现在我的下面是我的代码
parser = make_parser() curHandler = BasketBallHandler() parser.setContentHandler(curHandler) for q in XMLdocs.all(): parser.parse(StringIO.StringIO(q.content))
我低于错误
'ascii' codec can't encode character u'\xef' in position 0: ordinal not in range(128) Traceback (most recent call last): File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/__init__.py", line 517, in __call__ handler.post(*groups) File "/base/data/home/apps/parsepython/1.348669006354245654/mapreduce/base_handler.py", line 59, in post self.handle() File "/base/data/home/apps/parsepython/1.348669006354245654/mapreduce/handlers.py", line 168, in handle scan_aborted = not self.process_entity(entity, ctx) File "/base/data/home/apps/parsepython/1.348669006354245654/mapreduce/handlers.py", line 233, in process_entity handler(entity) File "/base/data/home/apps/parsepython/1.348669006354245654/parseXML.py", line 71, in process parser.parse(StringIO.StringIO(q.content)) File "/base/python_runtime/python_dist/lib/python2.5/xml/sax/expatreader.py", line 107, in parse xmlreader.IncrementalParser.parse(self, source) File "/base/python_runtime/python_dist/lib/python2.5/xml/sax/xmlreader.py", line 123, in parse self.feed(buffer) File "/base/python_runtime/python_dist/lib/python2.5/xml/sax/expatreader.py", line 207, in feed self._parser.Parse(data, isFinal) File "/base/data/home/apps/parsepython/1.348669006354245654/parseXML.py", line 136, in characters print ch UnicodeEncodeError: 'ascii' codec can't encode character u'\xef' in position 0: ordinal not in range(128)
看来您要达到UTF-8字节顺序标记(BOM)。尝试使用此Unicode字符串并提取出BOM:
import codecs content = unicode(q.content.strip(codecs.BOM_UTF8), 'utf-8') parser.parse(StringIO.StringIO(content))
我使用strip而不是lstrip因为在您的情况下您可能多次出现BOM表,这可能是由于串联的文件内容引起的。
strip
lstrip