如何将花式/艺术化的Unicode文本转换为ASCII?

5

我有一个 Unicode 字符串,如 " ",想将其转换为 ASCII 格式 "thug life"。

我知道可以在 Python 中通过以下方法实现:

import unidecode
print(unidecode.unidecode(' '))
// thug life

然而,这样做也会将其他Unicode字符(例如中文/日文字符、表情符号、重音字符等)转换为ASCII字符,而我希望保留这些字符。

有没有一种方法可以检测这些“艺术性”的Unicode字符?

以下是一些更多的例子:

thug life

感谢您的帮助!


1
请参考NFC/NFD/NFKD/NFKC ''..normalize() - dakab
1个回答

6
import unicodedata
strings = [
  ' ',
  ' ',
  ' ',
  ' ',
  'thug life']
for x in strings:
  print(unicodedata.normalize( 'NFKC', x), x)

输出: .\62803325.py

thug life  
thug life  
thug life  
thug life  
thug life thug life

资源


1
不适用于以下情况:" ","︎︎︎︎","ωεłł","ʜᴀᴋsʜɪ","","","RᗅIPႮ","ғʀᴇᴇ","ᕼᗩᑭᑭIᗴᗴ","υεεɴ"。 - Gokul NC
1
@GokulNC 你需要Unicode文本的罗马化/音译,在我看来... 另请参阅 https://pypi.org/project/Unidecode/ - JosefZ
@GokulNC,你有没有想到用Python将这些字体转换为Unicode?如果是,请在此处评论您的方法。 - Naveen Reddy Marthala
如上所述,“Unidecode” 能够清除其中大部分,但它并没有完全 100% 解决问题。 - Gokul NC
是的,你说得对。所以我正在寻找外观转换的方法,而不是近似音转换。 - Gokul NC
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接