001_023 Python 对Unicode数据编码并应用于XML和HTML

代码如下:

#encoding=utf-8

print '中国'

#对Unicode数据编码并应用于XML和HTML

#解决方案
def encode_for_xml(udata,encoding='ascii'):
    return udata.encode(encoding,'xmlcharrefreplace')

print encode_for_xml(u'中国')

打印结果如下:

中国
中国

你可能感兴趣的:(001_023 Python 对Unicode数据编码并应用于XML和HTML)