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

Unicode 編碼錯誤 Python - 解析 XML 無法編碼字符(星

Unicode Encoding Errors Python - Parsing XML can#39;t encode a character (Star)(Unicode 編碼錯誤 Python - 解析 XML 無法編碼字符(星號))
本文介紹了Unicode 編碼錯誤 Python - 解析 XML 無法編碼字符(星號)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我是 Python 的初學者,目前正在從 eventful.com API 解析一個基于 Web 的 XML 文件,但是,在檢索數據的某些元素時,我收到了一些 unicode 錯誤.

I am a beginner to Python and am currently parsing a web-based XML file from the eventful.com API however, I am receiving some unicode errors when retrieving certain elements of the data.

我能夠從 xml 文件中檢索 5 個數據元素而沒有任何我想要的問題,但是它會終止并在 GAE 錯誤控制臺中產生以下錯誤:

I am able to retrieve 5 data elements without any problems which I want from the xml file, however then it terminates and produces the following error in the GAE error console:

UnicodeEncodeError: 'ascii' codec can't encode character u'u2605' in position 0: ordinal not in range(128)

我知道拋出我的解析器的字符是★"字符,無論如何我都不想從 xml 文件中檢索它.

I know that the character that is throwing my parser is a "★" character, which I would prefer to not retrieve from the xml file anyway.

我的代碼如下:

class XMLParser(webapp2.RequestHandler):
        def get(self):
        base_url = 'my xml file'
        #downloads data from xml file
        response = urllib.urlopen(base_url)
        #converts data to string:
        data = response.read()

        #closes file
        response.close()

        #parses xml downloaded
        dom = mdom.parseString(data)
        node = dom.documentElement  
        #print out all event names (titles) found in the eventful xml
        event_main = dom.getElementsByTagName('event')

        event_names = []
        for event in event_main:
            eventObj = event.getElementsByTagName("title")[0]
            event_names.append(eventObj)

        for ev in event_names:
            nodes = ev.childNodes
            for node in nodes:
                if node.nodeType == node.TEXT_NODE:
                    print node.data

有什么方法可以檢索標題"元素并忽略此處的 ★ 字符等有趣字符?我真的很感激在這件事上的任何幫助.我已經嘗試過使用 word.encode('us-ascii', 'ignore') 的解決方案,但這并不能解決問題.

Is there any way that I would be able to retrieve the "title" elements and ignore funny characters like the ★ character here? I would really appreciate any help on this matter. I have already tried solutions which uses word.encode('us-ascii', 'ignore') but this is not fixing the issue.

-----------我找到了解決方案:

-----------I HAVE FOUND THE SOLUTION:

因此,當我遇到此類問題時,在與該主題的講師交談后,我發(fā)現只需要兩行代碼即可對已解析的 xml 文件進行編碼和解碼(在讀取后進入程序).希望這可以幫助遇到同樣問題的其他人!

So as I was having such issues with this problem and after talking to a lecturer on this topic I was able to find that all it required was two lines of code to both encode and decode the parsed xml file (after it was read into the program). Hope this helps someone else having the same issue!

unicode_data = data.decode('utf-8')
data = unicode_data.encode('ascii','ignore')

推薦答案

你在哪里使用你的解碼方法?

Where are you using your decoding methods?

我過去遇到過這個錯誤,不得不解碼原始數據.換句話說,我會嘗試做

I had this error in the past and had to decode the raw. In other words, I would try doing

data = response.read()
#closes file
response.close()
#decode
data.encode("us-ascii")

也就是說,如果它實際上是 ascii.我的意思是,在調用 parseString 之前,請確保在原始結果仍為字符串格式時對其進行編碼/解碼.

That is if it is in fact ascii. My point being make sure you are encoding/decoding the raw results while it is still in a string format, before you call parseString on it.

這篇關于Unicode 編碼錯誤 Python - 解析 XML 無法編碼字符(星號)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時出現問題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節(jié)點)
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| 国精产品99永久一区一区 | 黄色av免费 | 欧美性受xxxx黑人xyx性爽 | 国产激情久久久 | 久久精品中文 | 国产尤物视频 | 日产精品久久久一区二区 | 国产激情在线 | 黄色一级大片在线免费看国产一 | 中文精品一区 | 欧美成人综合 | 免费毛片视频 | 久久精品99久久久久久 | 三级a毛片 | 欧美视频亚洲视频 | 日韩中文字幕免费 | 日本中文在线观看 | 亚洲精品黄 | 国产美女自拍视频 | 日韩视频专区 | 国产激情在线 | 人人爽人人爽人人 | 又大又黄又爽 | 日韩一区二区在线视频 | 婷婷一区二区三区 | 久久精品欧美一区二区三区不卡 | 自由成熟xxxx色视频 | 久久黄色免费视频 | a级片网址 | 成人午夜精品 | 欧美日韩免费视频 | 特级西西444www大胆免费看 | www国产在线观看 | 国产成人精品三级麻豆 | 激情五月综合色婷婷一区二区 | 国产午夜麻豆影院在线观看 | 一区二区三区视频在线播放 |