本文介绍了UnicodeEncodeError:'ascii'编解码器不能在位置0编码字符'u'\xef':序号不在范围内(128)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想解析我的XML文档。所以我已经存储了我的XML文档,如下所示:
class XMLdocs(db.Expando):
id = db.IntegerProperty ()
name = db.StringProperty()
content = db.BlobProperty()
现在我的下面是我的代码
pre $
parser = make_parser()
curHandler = BasketBallHandler()
)parser.setContentHandler(curHandler)
for XMLdocs.all():
parser.parse(StringIO.StringIO(q.content))
我得到的错误
'ascii'codec can ''在位置0中编码字符u'\xef':序号不在范围内(128)
追踪(最近调用最后一个):
文件/ base / python_runtime / python_lib / versions / 1 / google / appengine / ext / webapp / __ init__.py,第517行,在__call__
handler.post(* groups)
文件/base/data/home/apps/parsepython/1.348669006354245654/mapreduce/ base_handler.py,第59行,后
self。 handle()
文件/base/data/home/apps/parsepython/1.348669006354245654/mapreduce/handlers.py,第168行,处理
scan_aborted = not self.process_entity(entity,ctx)
文件/base/data/home/apps/parsepython/1.348669006354245654/mapreduce/handlers.py,第233行,在process_entity
处理程序(实体)
文件/ base / data / home /apps/parsepython/1.348669006354245654/parseXML.py,第71行,正在处理
parser.parse(StringIO.StringIO(q.content))
文件/ base / python_runtime / python_dist / lib / python2 .5 / xml / sax / expatreader.py,第107行,解析
xmlreader.IncrementalParser.parse(self,source)
文件/base/python_runtime/python_dist/lib/python2.5/ xml / sax / xmlreader.py,第123行,解析
self.feed(buffer)
文件/base/python_runtime/python_dist/lib/python2.5/xml/sax/expatreader.py ,第207行,在feed
self._parser.Parse(data,isFinal)
文件/base/data/home/apps/parsepython/1.348669006354245654 /parseXML.py,第136行,以字符为单位
打印ch
UnicodeEncodeError:'ascii'编解码器无法在位置0编码字符u'\xef':序号不在范围内(128)
解决方案
看起来您正在创建一个UTF-8字节顺序标记(BOM)。尝试使用这个带有BOM的unicode字符串提取出来:
import codecs
content = unicode(q。 content.strip(codecs.BOM_UTF8),'utf-8')
parser.parse(StringIO.StringIO(content))
我使用 strip
而不是 lstrip
,因为在您的情况下,您有多次出现物料清单,可能是由于连接的文件内容。
I want to parse my XML document. So I have stored my XML document as below
class XMLdocs(db.Expando):
id = db.IntegerProperty()
name=db.StringProperty()
content=db.BlobProperty()
Now my below is my code
parser = make_parser()
curHandler = BasketBallHandler()
parser.setContentHandler(curHandler)
for q in XMLdocs.all():
parser.parse(StringIO.StringIO(q.content))
I am getting below error
'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)
解决方案
It seems you are hitting a UTF-8 byte order mark (BOM). Try using this unicode string with BOM extracted out:
import codecs
content = unicode(q.content.strip(codecs.BOM_UTF8), 'utf-8')
parser.parse(StringIO.StringIO(content))
I used strip
instead of lstrip
because in your case you had multiple occurences of BOM, possibly due to concatenated file contents.
这篇关于UnicodeEncodeError:'ascii'编解码器不能在位置0编码字符'u'\xef':序号不在范围内(128)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!