简体   繁体   English

如何检查字符串中的unicode或转义序列?

[英]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\\xb6LNKö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.

相关问题 如何将unicode转义序列转换为python字符串中的unicode字符 - How do convert unicode escape sequences to unicode characters in a python string Python:Unicode字符串和转义序列 - Python: Unicode string and Escape sequences 如何从 python 中的字符串中删除 ANSI 转义序列 - How can I remove the ANSI escape sequences from a string in python 从 Python 到 Java 我怎样才能做十六进制转义序列? - From Python to Java how can I do hex escape sequences? Is there a built in python function to escape a string of unicode characters into unicode escape sequences “\uXXXX\uXXXX\uXXXX”? - Is there a built in python function to escape a string of unicode characters into unicode escape sequences “\uXXXX\uXXXX\uXXXX”? 如何在python中转义UNICODE字符串(到javascript转义) - How to escape UNICODE string in python (to javascript escape) 将 unicode 字符编码为 un​​icode 转义序列 - encode unicode characters to unicode escape sequences 如何检查Python unicode字符串以确定它* *实际上是*正确的Unicode? - How can I check a Python unicode string to see that it *actually* is proper Unicode? 如何检查字符串是 unicode 还是 ascii? - How do I check if a string is unicode or ascii? 如何提取包含转义序列(如 \a,\16)的输入字符串文字作为原始字符串? - how do I extract a input string literal containing escape sequences like \a,\16 as a raw string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM