如何在Python(给定perl脚本)中用ASCII字符替换unicode字符? [英] How to replace unicode characters by ascii characters in Python (perl script given)?

查看:101
本文介绍了如何在Python(给定perl脚本)中用ASCII字符替换unicode字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习python,却不知道如何将以下perl脚本转换为python:

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;
}

该脚本仅将Unicode变音符更改为替代的ascii输出. (所以完整的输出是ascii.)对于任何提示,我将不胜感激.谢谢!

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!

推荐答案

  • 使用 fileinput 模块循环遍历标准输入或文件列表,
  • 将您从UTF-8读取的行解码为unicode对象
  • 然后使用 translate 方法
    • Use the fileinput module to loop over standard input or a list of files,
    • decode the lines you read from UTF-8 to unicode objects
    • then map any unicode characters you desire with the translate method
    • translit.py看起来像这样:

      #!/usr/bin/env python2.6
      # -*- coding: utf-8 -*-
      
      import fileinput
      
      table = {
                0xe4: u'ae',
                ord(u'ö'): u'oe',
                ord(u'ü'): u'ue',
                ord(u'ß'): None,
              }
      
      for line in fileinput.input():
          s = line.decode('utf8')
          print s.translate(table), 
      

      您可以这样使用它:

      $ cat utf8.txt 
      sömé täßt
      sömé täßt
      sömé täßt
      
      $ ./translit.py utf8.txt 
      soemé taet
      soemé taet
      soemé taet
      

      • 更新:
      • 如果您使用的是python 3字符串,则默认情况下是unicode,并且如果它包含非ASCII字符甚至是非拉丁字符,则不需要对其进行编码.因此,解决方案如下所示:

        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:

        line = 'Verhältnismäßigkeit, Möglichkeit'
        
        table = {
                 ord('ä'): 'ae',
                 ord('ö'): 'oe',
                 ord('ü'): 'ue',
                 ord('ß'): 'ss',
               }
        
        line.translate(table)
        
        >>> 'Verhaeltnismaessigkeit, Moeglichkeit'
        

        这篇关于如何在Python(给定perl脚本)中用ASCII字符替换unicode字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆