[英]How can I check for unicode or escape sequences in a string?
I have a dictionary list of words, some of the words containing sequences like so: 我有一个单词词典列表,其中一些单词包含如下序列:
K\\xc3\\xb6LN
or KöLN
when displayed properly. 正确显示时为K\\xc3\\xb6LN
或KöLN
。
I'd like to purge the list of such words, such that they contain plain ascii characters only. 我想清除此类单词的列表,以使它们仅包含纯ascii字符。 How can I do a simple True/False check to see if a string contains such sequences? 如何进行简单的True / False检查字符串是否包含此类序列?
str.isalpha()
may be of assistance here: str.isalpha()
在这里可能会有所帮助:
>>> 'KöLN'.isalpha()
False
>>> 'K\xc3\xb6LN'.isalpha()
False
>>> 'Cologne'.isalpha()
True
Filtering: 过滤条件:
>>> [word for word in ('KöLN', 'K\xc3\xb6LN', 'Cologne') if word.isalpha()]
['Cologne']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.