如何检测 python 中字符串中的 ASCII 字符
How to detect ASCII characters on a string in python
我正在开发 Maya 中的一个工具,在某些时候,用户可以在文本字段中输入评论。此评论稍后将用作要保存的文件名的一部分。
我在法国工作,所以用户可能会使用一些重音字符,如“é”或“à”。
我想要的是将它们翻译成它们未强调的对应字符。但是我意识到这很棘手,所以我可以接受 juste 检测它们,这样我就可以向用户发出警告消息。我不想只删除有罪的字母,因为这可能会导致评论难以理解。
我知道这里有一些类似的问题,但它们都是关于我不了解的其他语言 know/understand(例如 C++ 或 php)。
以下是我目前在网上找到的内容:
import re
comment = 'something written with some french words and numbers'
if re.match(r'^[A-Za-z0-9_]+$', text):
# issue a warning for the user
第一个解决方案不起作用,因为它认为重音字符是可以接受的。
我找到了这个:
ENGLISH_CHARS = re.compile('[^\W_]', re.IGNORECASE)
ALL_CHARS = re.compile('[^\W_]', re.IGNORECASE | re.UNICODE)
assert len(ENGLISH_CHARS.findall('_àÖÎ_')) == 0
assert len(ALL_CHARS.findall('_àÖÎ_')) == 3
我想像这样使用:
ENGLISH_CHARS = re.compile('[^\W_]', re.IGNORECASE)
if len(ENGLISH_CHARS .findall(comment)) != len(comment):
# issue a warning for the user
但它似乎只有在字符串封装在下划线中时才有效。
如果这是我没有找到或理解的东西的副本,我真的很抱歉,但这让我抓狂。
unicode 命令尝试以给定的编码对您的字符串进行编码。它将默认为 ASCII 并在失败时引发异常。
try:
unicode(filename)
except UnicodeDecodeError:
show_warning()
这只允许无重音字符,这可能是您想要的。
如果您已有 Unicode 字符串,则必须更改编码,这将引发 UnicodeEncodeError。
filename.encode("ASCII")
示例:
>>> unicode("ää")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)
看来你其实有两个问题
如何发现是否需要从 ASCII 将重音字符转换为 'similar'。
#coding: utf-8
import string
text = u"Montréal, über, 12.89, Mère, Françoise, noël, 889"
allowed_letters = string.printable
name_has_accented = [letter for letter in text if not letter in allowed_letters]
if name_has_accented:
text = "".join(convert(text))
print(text)
如何轻松地将它们转换为非重音?你可以设计出很好的通用解决方案,或者你可以只为法语做,就像这样很容易:
def convert(text):
replacements = {
u"à": "a",
u"Ö": "o",
u"é": "e",
u"ü": "u",
u"ç": "c",
u"ë": "e",
u"è": "e",
}
def convert_letter(letter):
try:
return replacements[letter]
except KeyError:
return letter
return [convert_letter(letter) for letter in text]
我正在开发 Maya 中的一个工具,在某些时候,用户可以在文本字段中输入评论。此评论稍后将用作要保存的文件名的一部分。
我在法国工作,所以用户可能会使用一些重音字符,如“é”或“à”。
我想要的是将它们翻译成它们未强调的对应字符。但是我意识到这很棘手,所以我可以接受 juste 检测它们,这样我就可以向用户发出警告消息。我不想只删除有罪的字母,因为这可能会导致评论难以理解。
我知道这里有一些类似的问题,但它们都是关于我不了解的其他语言 know/understand(例如 C++ 或 php)。
以下是我目前在网上找到的内容:
import re
comment = 'something written with some french words and numbers'
if re.match(r'^[A-Za-z0-9_]+$', text):
# issue a warning for the user
第一个解决方案不起作用,因为它认为重音字符是可以接受的。
我找到了这个:
ENGLISH_CHARS = re.compile('[^\W_]', re.IGNORECASE)
ALL_CHARS = re.compile('[^\W_]', re.IGNORECASE | re.UNICODE)
assert len(ENGLISH_CHARS.findall('_àÖÎ_')) == 0
assert len(ALL_CHARS.findall('_àÖÎ_')) == 3
我想像这样使用:
ENGLISH_CHARS = re.compile('[^\W_]', re.IGNORECASE)
if len(ENGLISH_CHARS .findall(comment)) != len(comment):
# issue a warning for the user
但它似乎只有在字符串封装在下划线中时才有效。
如果这是我没有找到或理解的东西的副本,我真的很抱歉,但这让我抓狂。
unicode 命令尝试以给定的编码对您的字符串进行编码。它将默认为 ASCII 并在失败时引发异常。
try:
unicode(filename)
except UnicodeDecodeError:
show_warning()
这只允许无重音字符,这可能是您想要的。
如果您已有 Unicode 字符串,则必须更改编码,这将引发 UnicodeEncodeError。
filename.encode("ASCII")
示例:
>>> unicode("ää")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)
看来你其实有两个问题
如何发现是否需要从 ASCII 将重音字符转换为 'similar'。
#coding: utf-8 import string text = u"Montréal, über, 12.89, Mère, Françoise, noël, 889" allowed_letters = string.printable name_has_accented = [letter for letter in text if not letter in allowed_letters] if name_has_accented: text = "".join(convert(text)) print(text)
如何轻松地将它们转换为非重音?你可以设计出很好的通用解决方案,或者你可以只为法语做,就像这样很容易:
def convert(text): replacements = { u"à": "a", u"Ö": "o", u"é": "e", u"ü": "u", u"ç": "c", u"ë": "e", u"è": "e", } def convert_letter(letter): try: return replacements[letter] except KeyError: return letter return [convert_letter(letter) for letter in text]