我需要从从 xml 文件获得的字符串中删除波兰语字符。我使用 .replace() 但在这种情况下它不起作用。为什么? 代码:
# -*- coding: utf-8
from prestapyt import PrestaShopWebService
from xml.etree import ElementTree
prestashop = PrestaShopWebService('http://localhost/prestashop/api',
'key')
prestashop.debug = True
name = ElementTree.tostring(prestashop.search('products', options=
{'display': '[name]', 'filter[id]': '[2]'}), encoding='cp852',
method='text')
print name
print name.replace('ł', 'l')
输出:
Naturalne mydło odświeżające
Naturalne mydło odświeżające
但是当我尝试替换非波兰字符时,它工作正常。
print name
print name.replace('a', 'o')
结果:
Naturalne mydło odświeżające
Noturolne mydło odświeżojące
这也很好:
name = "Naturalne mydło odświeżające"
print name.replace('ł', 'l')
有什么建议吗?
请您参考如下方法:
如果我理解正确你的问题,你可以使用unidecode :
>>> from unidecode import unidecode
>>> unidecode("Naturalne mydło odświeżające")
'Naturalne mydlo odswiezajace'
您可能必须先使用 name.decode('utf_8') 解码您的 cp852 编码字符串。
