作者 主題: 請問Python的XML編碼,'xmlcharrefreplace'  (閱讀 13329 次)

0 會員 與 1 訪客 正在閱讀本文。

fofofo

  • 可愛的小學生
  • *
  • 文章數: 1
    • 檢視個人資料
Python版本:2.3

請問若是我將一字串從unicode轉成XML格式的字元編碼,如何再重XML格式編碼轉成unicode呢?
如:

text = u"1月1日"
text2 = text.encode('ascii','xmlcharrefreplace')
print text2   #顯示    1月1日

請問我要怎樣做才能將[1月1日]字串變回"1月1日"呢?

謝謝

flylinux

  • 可愛的小學生
  • *
  • 文章數: 8
    • 檢視個人資料
    • 工作達人
回覆: 請問Python的XML編碼,'xmlcharrefreplace'
« 回覆 #1 於: 2010-08-05 15:32 »
用Beautiful Soup模組!
# -*- coding: utf-8 -*-
from BeautifulSoup import BeautifulStoneSoup
text = u"1月1日"
text2 = text.encode('ascii','xmlcharrefreplace')
print text2

decodedString=unicode(BeautifulStoneSoup(text2,convertEntities=BeautifulStoneSoup.HTML_ENTITIES ))
print decodedString
工作達人
幫我衝Fun Taiwan連結,有機會中飲料!