2017-02-05 3 views
1
string = "Special $#! characters spaces 888323 Kek ཌི ༜ 郭 ༜ དྀ " 

Ergebnis sein sollte: "Specialcharactersspaces888323Kek 郭"Python 2.7 entfernen spezielle chac, Abstand ABER NICHT CHINESE CHARACTERS

ich versucht habe mit
print ''.join(c for c in string.decode('utf-8') if u'\u4e00' <= c <= u'\u9fff')

aber Fehler Rückkehr
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python27\lib\encodings\utf_8.py", line 16, in decode return codecs.utf_8_decode(input, errors, True) UnicodeEncodeError: 'ascii' codec can't encode character u'\u90ed' in position 4 9: ordinal not in range(128)

meine Frage gleich wie Titel,
spezielle chac entfernen, Abstand ABER NICHT CHINESE CHARACTERS

Antwort

1

Die Lösung mit re.compile und re.sub Funktionen:

import re 

string = "Special $#! characters spaces 888323 Kek ཌི ༜ 郭 ༜ དྀ " 

# defining the pattern which should match all characters excepting alphanumeric and chinese 
pattern = re.compile(u'[^a-z0-9⺀-⺙⺛-⻳⼀-⿕々〇〡-〩〸-〺〻㐀-䶵一-鿃豈-鶴侮-頻並-龎]', re.UNICODE | re.IGNORECASE) 
result = pattern.sub('', string) 

# print(result) Python v.3 printing 
print result 

Der Ausgang:

Specialcharactersspaces888323Kek郭 
+0

wie über ich möchte nicht Symbol wie '$ # !? <>' @RomanPerekhrest –

+0

Ausnahme Sonder Chac wie! @ # $%^& *(): "<> /. –

+0

@ChinYe, zeige das erwartete Ergebnis gemäß deiner "Ausnahmeliste" – RomanPerekhrest