python - 如何在 Python 中用 ascii 字符替换 unicode 字符(给定的 perl 脚本)?

标签 python perl unicode diacritics

我正在尝试学习 python,但不知道如何将以下 perl 脚本翻译成 python:

#!/usr/bin/perl -w                     

use open qw(:std :utf8);

while(<>) {
  s/\x{00E4}/ae/;
  s/\x{00F6}/oe/;
  s/\x{00FC}/ue/;
  print;
}

脚本只是将 unicode 变音符号更改为替代 ascii 输出。 (所以完整的输出在 ascii 中。)我将不胜感激任何提示。谢谢!

最佳答案

要转换为 ASCII,您可能需要尝试 ASCII, Dammitthis recipe ,归结为:

>>> title = u"Klüft skräms inför på fédéral électoral große"
>>> import unicodedata
>>> unicodedata.normalize('NFKD', title).encode('ascii','ignore')
'Kluft skrams infor pa federal electoral groe'

关于python - 如何在 Python 中用 ascii 字符替换 unicode 字符(给定的 perl 脚本)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2700859/

相关文章:

python - 同时运行多个相互通信的 Kivy 应用程序

perl - 如何模拟内置 `require` 函数?

jquery - 不区分重音的正则表达式

python - Unicode 转义文件处理错误

java - FontFactory (lowagie),Java,尝试使用 UniJIS-UCS2-H(日语)时出现 UnsupportedEncodingException

python - 在 Windows 上安装 Darknet 时出错

python ftplib : How to store results of `FTP.retrlines` in a list?

Python 按模式列出 Azure Blob

perl - 为什么 00 在 perl 中等于 0?

perl - ShipIt 与 Dist::Zilla 的优势/劣势是什么?