如何在Python中用ascii字符替换unicode字符(给出perl脚本)?
我正在尝试学习 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 格式的。)如果有任何提示,我将不胜感激。谢谢!
I am trying to learn python and couldn't figure out how to translate the following perl script to python:
#!/usr/bin/perl -w
use open qw(:std :utf8);
while(<>) {
s/\x{00E4}/ae/;
s/\x{00F6}/oe/;
s/\x{00FC}/ue/;
print;
}
The script just changes unicode umlauts to alternative ascii output. (So the complete output is in ascii.) I would be grateful for any hints. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
要转换为 ASCII,您可能需要尝试 ASCII,该死 或这个食谱,归结为:
For converting to ASCII you might want to try ASCII, Dammit or this recipe, which boils down to:
fileinput
模块循环标准输入或列表文件,translate
方法translit.py
看起来像这样:你可以这样使用它:
如果您使用 python 3 字符串,默认情况下是 unicode,如果它包含非 ASCII 字符甚至非拉丁字符,则不需要对其进行编码。所以解决方案如下所示:
fileinput
module to loop over standard input or a list of files,translate
methodtranslit.py
would look like this:And you could use it like this:
In case you are using python 3 strings are by default unicode and you dont' need to encode it if it contains non-ASCII characters or even a non-Latin characters. So the solution will look as follow:
您可以尝试
unidecode
将 Unicode 转换为 ascii,而不是手动编写正则表达式。它是Text::Unidecode
Perl 模块的 Python 端口:它使用
FileInput
类来避免全局状态。例子:
You could try
unidecode
to convert Unicode into ascii instead of writing manual regular expressions. It is a Python port ofText::Unidecode
Perl module:It uses
FileInput
class to avoid global state.Example:
我使用 translitcodec
您可以将解码语言更改为您需要的任何语言。您可能需要一个简单的函数来减少单个实现的长度。
I use translitcodec
You can change the decode language to whatever you need. You may want a simple function to reduce length of a single implementation.
又快又脏(python2):
Quick and dirty (python2):