簡體   English   中英

Python將Unicode保存為XML

[英]Python saving unicode to XML

我目前正在編寫一個簡短的Python腳本,以遍歷服務器上的某些目錄,查找所需內容,並將數據保存到XML文件中。

問題在於某些數據是以其他語言編寫的,例如“ハローワールド”或類似格式的東西。 嘗試將其保存在XML的條目中時,得到以下回溯:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xec in position 18: ordinal not in range(128)

這就是我保存數據的函數的樣子:

def addHistoryEntry(self, title, url):
    self.log.info('adding history entry {"title":"%s", "url":"%s"}' % (title, url))
    root = self.getRoot()
    history = root.find('.//history')
    entry = etree.SubElement(history, 'entry')
    entry.set('title', title)
    entry.set('time', str(unixtime()))
    entry.text = url
    history.set('results', str(int(history.attrib['results']) + 1))
    self.write(root)

self.getRoot()如下:

def getRoot(self):
    return etree.ElementTree(file = self.config).getroot()

這是寫入數據的函數( self.write(root)

def write(self, xmlRoot):
    bump = open(self.config, 'w+')
    bump.write(dom.parseString(etree.tostring(xmlRoot, 'utf-8')).toxml())
    bump.close()

進口是:

import xml.etree.ElementTree as etree
import xml.dom.minidom as dom

如果有人可以幫助我解決此問題,請執行。 謝謝你的幫助。

您可以使用python的編解碼器庫和.decode('utf-8')解決該問題。

import sys,codecs
reload(sys)
sys.setdefaultencoding("utf-8")

默認情況下,python會將unicode編碼為ASCII。 由於ASCII僅支持128個字符,因此它將引發超過128個異常值。因此,解決方案是將unicode編碼為擴展格式,例如latin1或UTF8或其他ISO字符集。 我建議在utf-8中編碼。 語法為:

u"Your unucode Text".encode("utf-8", "ignore")

為了你

title.encode("utf-8", "ignore")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM