久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

UnicodeEncodeError:'ascii' 編解碼器無法在位置

UnicodeEncodeError: #39;ascii#39; codec can#39;t encode character u#39;xef#39; in position 0: ordinal not in range(128)(UnicodeEncodeError:ascii 編解碼器無法在位置 0 編碼字符 uxef:序數不在范圍內(128)) - IT屋-程序員軟件
本文介紹了UnicodeEncodeError:'ascii' 編解碼器無法在位置 0 編碼字符 u'xef':序數不在范圍內(128)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想解析我的 XML 文檔.所以我將我的 XML 文檔存儲如下

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))

我遇到了錯誤

'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).嘗試使用這個帶有 BOM 提取的 unicode 字符串:

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))

我使用 strip 而不是 lstrip 因為在您的情況下,您多次出現 BOM,可能是由于連接的文件內容.

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)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時出現問題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節點)
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應并將元素值存儲在變量中?)
How to get XML tag value in Python(如何在 Python 中獲取 XML 標記值)
How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正確解析 utf-8 xml?)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對象)
主站蜘蛛池模板: 九九热免费在线观看 | av在线播放免费 | 九九av | 精品一区二区三区四区五区 | 黄色毛片在线看 | 久久综合一区二区三区 | 久久国际精品 | 91麻豆产精品久久久久久夏晴子 | av日韩高清 | 日韩成人在线播放 | 国产精品久久久久国产a级 欧美日本韩国一区二区 | 亚洲精选久久 | 午夜一区二区三区 | 亚洲一区二区三区四区五区中文 | 午夜小视频在线观看 | 欧美综合一区 | 97伦理电影| 午夜看电影在线观看 | 日韩欧美1区2区 | 亚洲午夜久久久 | 一区二区视屏 | 97国产精品 | 国产在线观看一区二区 | 一区二区av | 日日做夜夜爽毛片麻豆 | 精品一二三区视频 | 三级黄色片在线 | 日韩h| 97久久精品 | 伊人伊人 | 91精品国模一区二区三区 | 国产一区二区精品在线观看 | 1000部精品久久久久久久久 | 国产资源一区二区三区 | 成人不卡视频 | 亚洲欧美一区二区三区1000 | 国产精品毛片av一区 | 日韩欧美在线视频 | 日本精品一区二区三区视频 | 九九天堂网 | 国产xxxx搡xxxxx搡麻豆 |