python中的字符编码将'u2019'替换为'

use*_*113 0 python unicode lxml character-encoding python-2.7

我已尝试过多种方法将其编码为最终结果"BACK RUSHIN'",最重要的字符是右撇号'.

我想要一种方法来使用Python中的一些内置函数来获得最终结果,其中正常字符串和unicode字符串之间没有区别.

这是我用来检索字符串的代码: str(unicode(etree.tostring(root.xpath('path')[0],method='text', encoding='utf-8'),errors='ignore')).strip()

结果是:缺少'BACK RUSHIN'撇号的东西'.

另一种方式是: root.xpath('path/text()')

结果是:u'BACK RUSHIN\u2019'在python中.

最后,如果我尝试: u'BACK RUSHIN\u2019'.encode('ascii', 'replace')

结果是: 'BACK RUSHIN?'

请不要替换函数,我想利用pythons编解码库.也没有打印字符串,因为它被保存在变量中.

谢谢

Ign*_*ams 7

>>> import unidecode
>>> unidecode.unidecode(u'BACK RUSHIN\u2019')
"BACK RUSHIN'"
Run Code Online (Sandbox Code Playgroud)

unidecode

  • 可能值得一提的是你必须安装`unidecode`. (6认同)