2017-09-16 7 views
2

私はxmlファイルから得た文字列から磨き文字を取り除く必要があります。 .replace()を使用しますが、この場合は動作しません。どうして? コード:文字列からいくつかの文字を取り除くには? .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') 

出力:

しかし、私はそれが正常に動作非ポリッシュの文字を交換してみてください。

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') 

任意のアドバイス?

+0

あなたは同じ[標準形](https://en.m.wikipedia.org/wiki/Unicode_equivalence)に両方の文字列のUnicodeの形を正規化する必要があります。 –

+0

[unicodedata.normalize(form、unistr)がどのようにしてどのように動作するかをsomoneで説明できますか?](https://stackoverflow.com/questions/14682397/can-somone-explain-how-unicodedata-normalizeform-unistr-work -with-examples) –

答えて

0

バイトストリングでエンコードをミックスしています。この問題を再現する簡単な実例があります。

#!python2 
# coding: utf-8 
from xml.etree import ElementTree as et 
name_element = et.Element('data') 
name_element.text = u'Naturalne mydło odświeżające' 
name = et.tostring(name_element,encoding='cp852', method='text') 
print name 
print name.replace('ł', 'l') 

出力(代替はありません)::で、name文字列がcp852でエンコードされた

理由が、私はあなたがcp852のエンコーディングにそのデフォルトはWindowsコンソールで実行されていると仮定しますバイト文字列定数'ł'は、utf-8のソースコードエンコードでエンコードされています。

print repr(name) 
print repr('ł') 

出力:

'Naturalne myd\x88o od\x98wie\xbeaj\xa5ce' 
'\xc5\x82' 

最善の解決策は、Unicode文字列を使用することです:

#!python2 
# coding: utf-8 
from xml.etree import ElementTree as et 
name_element = et.Element('data') 
name_element.text = u'Naturalne mydło odświeżające' 
name = et.tostring(name_element,encoding='cp852', method='text').decode('cp852') 
print name 
print name.replace(u'ł', u'l') 
print repr(name) 
print repr(u'ł') 

出力(交換が行われた):

Naturalne mydło odświeżające 
Naturalne mydlo odświeżające 
u'Naturalne myd\u0142o od\u015bwie\u017caj\u0105ce' 
u'\u0142' 

注Pythonの3のことet.tostringにはUnicodeオプションがあり、文字列定数はデフォルトでUnicodeです。文字列のrepr()バージョンも同様に読みやすいですが、ascii()は古い動作を実装しています。また、Python 3.6がポーランド語のコードページを使用しないコンソールにもポーランド語を印刷することになるので、文字をまったく置き換える必要はありません。

#!python3 
# coding: utf-8 
from xml.etree import ElementTree as et 
name_element = et.Element('data') 
name_element.text = 'Naturalne mydło odświeżające' 
name = et.tostring(name_element,encoding='unicode', method='text') 
print(name) 
print(name.replace('ł','l')) 
print(repr(name),repr('ł')) 
print(ascii(name),ascii('ł')) 

出力:

Naturalne mydło odświeżające 
Naturalne mydlo odświeżające 
'Naturalne mydło odświeżające' 'ł' 
'Naturalne myd\u0142o od\u015bwie\u017caj\u0105ce' '\u0142' 
+0

ありがとう!エンコード/デコードのことはまだまだやや難解なので、Unicode Howtoを勉強しなければならないと思います。また、Python 3.xに移行することも検討します。 – vex

2

私が正しくあなたの問題を理解していれば、あなたはunidecodeを使用することができます。

>>> from unidecode import unidecode 
>>> unidecode("Naturalne mydło odświeżające") 
'Naturalne mydlo odswiezajace' 

あなたが最初name.decode('utf_8')であなたCP852エンコードされた文字列をデコードする必要がある場合があります。

+1

ありがとう!私はあなたの甘やかさを実装して、今はすべて正常に動作します。 – vex

関連する問題