【问题标题】:Is there an existing mapping from utf8 to latin-1? Python是否存在从 utf8 到 latin-1 的映射? Python
【发布时间】:2013-02-04 07:02:35
【问题描述】:

在 latin-1 和 utf8 中是否存在从 utf8 到标准化非重音字母的映射?

我遇到了以下错误:

UnicodeEncodeError: 'latin-1' codec can't encode character u'\u010d' in position 4: ordinal not in range(256)

我正在通过执行以下代码手动解决这些错误中的每一个。有没有更好的方法来做到这一点?:

def prehunpos(sentence):
    sentence = sentence.replace(u'\u2018',"'") # left single quote mark
    sentence = sentence.replace(u'\u2019',"'") # right single quote mark
    sentence = sentence.replace(u'\u201C','"') # left double quote mark
    sentence = sentence.replace(u'\u201D','"') # right double quote mark
    sentence = sentence.replace(u'\u2010',"-") # hyphen
    sentence = sentence.replace(u'\u2011',"-") # non-break hyphen
    sentence = sentence.replace(u'\u2012',"-") # figure dash
    sentence = sentence.replace(u'\u2013',"-") # dash
    sentence = sentence.replace(u'\u2014',"-") # some sorta dash
    sentence = sentence.replace(u'\u2015',"-") # long dash
    sentence = sentence.replace(u'\u2017',"_") # double underscore
    sentence = sentence.replace(u'\u2014',"-") # some sorta dash
    sentence = sentence.replace(u'\u2016',"|") # long dash
    sentence = sentence.replace(u'\u2024',"...") # ...
    sentence = sentence.replace(u'\u2025',"...") # ...
    sentence = sentence.replace(u'\u2026',"...") # ...
    sentence = sentence.replace("\xce\x9d\xce\x91\xce\xa4\xce\x9f",u'NATO') # NATO

    sentence = sentence.replace(u'\u0391',"A") # Greek Capital Alpha
    sentence = sentence.replace(u'\u0392',"B") # Greek Capital Beta
    #sentence = sentence.replace(u'\u0393',"") # Greek Capital Gamma
    #sentence = sentence.replace(u'\u0394',"") # Greek Capital Delta
    sentence = sentence.replace(u'\u0395',"E") # Greek Capital Epsilon
    sentence = sentence.replace(u'\u0396',"Z") # Greek Capital Zeta
    sentence = sentence.replace(u'\u0397',"H") # Greek Capital Eta
    #sentence = sentence.replace(u'\u0398',"") # Greek Capital Theta
    sentence = sentence.replace(u'\u0399',"I") # Greek Capital Iota
    sentence = sentence.replace(u'\u039a',"K") # Greek Capital Kappa
    #sentence = sentence.replace(u'\u039b',"") # Greek Capital Lambda
    sentence = sentence.replace(u'\u039c',"M") # Greek Capital Mu
    sentence = sentence.replace(u'\u039d',"N") # Greek Capital Nu
    #sentence = sentence.replace(u'\u039e',"") # Greek Capital Xi
    sentence = sentence.replace(u'\u039f',"O") # Greek Capital Omicron
    sentence = sentence.replace(u'\u03a1',"P") # Greek Capital Rho
    #sentence = sentence.replace(u'\u03a3',"") # Greek Capital Sigma
    sentence = sentence.replace(u'\u03a4',"T") # Greek Capital Tau
    sentence = sentence.replace(u'\u03a5',"Y") # Greek Capital Upsilon
    #ssentence = sentence.replace(u'\u03a6',"") # Greek Capital Phi
    sentence = sentence.replace(u'\u03a7',"T") # Greek Capital Chi
    #sentence = sentence.replace(u'\u03a8',"") # Greek Capital Psi
    #sentence = sentence.replace(u'\u03a9',"") # Greek Capital Omega

    sentence = sentence.replace(u'\u03b1',"a") # Greek small alpha
    sentence = sentence.replace(u'\u03b2',"b") # Greek small beta
    #sentence = sentence.replace(u'\u03b3',"") # Greek small gamma
    #sentence = sentence.replace(u'\u03b4',"") # Greek small delta
    sentence = sentence.replace(u'\u03b5',"e") # Greek small epsilon
    #sentence = sentence.replace(u'\u03b6',"") # Greek small zeta
    #sentence = sentence.replace(u'\u03b7',"") # Greek small eta
    #sentence = sentence.replace(u'\u03b8',"") # Greek small thetha
    sentence = sentence.replace(u'\u03b9',"i") # Greek small iota
    sentence = sentence.replace(u'\u03ba',"k") # Greek small kappa
    #sentence = sentence.replace(u'\u03bb',"") # Greek small lamda
    sentence = sentence.replace(u'\u03bc',"u") # Greek small mu
    sentence = sentence.replace(u'\u03bd',"v") # Greek small nu
    #sentence = sentence.replace(u'\u03be',"") # Greek small xi
    sentence = sentence.replace(u'\u03bf',"o") # Greek small omicron
    #sentence = sentence.replace(u'\u03c0',"") # Greek small pi
    sentence = sentence.replace(u'\u03c1',"p") # Greek small rho
    sentence = sentence.replace(u'\u03c2',"c") # Greek small final sigma
    #sentence = sentence.replace(u'\u03c3',"") # Greek small sigma
    sentence = sentence.replace(u'\u03c4',"t") # Greek small tau
    sentence = sentence.replace(u'\u03c5',"u") # Greek small upsilon
    #sentence = sentence.replace(u'\u03c6',"") # Greek small phi
    sentence = sentence.replace(u'\u03c7',"x") # Greek small chi
    sentence = sentence.replace(u'\u03c8',"x") # Greek small psi
    sentence = sentence.replace(u'\u03c9',"w") # Greek small omega


    sentence = sentence.replace(u'\u0103',"a") # Latin a with breve
    sentence = sentence.replace(u'\u0107',"c") # Latin c with acute
    sentence = sentence.replace(u'\u010d',"c") # Latin c with caron
    sentence = sentence.replace(u'\u0161',"s") # Lation s with caron

    return sentence.strip()

【问题讨论】:

标签: python unicode encoding utf-8 latin1


【解决方案1】:

如果您需要将非拉丁文字转换为拉丁文字的通用方法,ICU transform 是最佳选择。 ICU 有一个 Python 包装器 PyICU (http://pypi.python.org/pypi/PyICU)。但是,如果您只针对单个脚本(看起来您对希腊语特别感兴趣?),映射表是最快的解决方案。虽然你可以写得更简洁:

#!/usr/bin/python
# -*- coding: utf-8 -*-

greek_to_latin = {u"Α": u"A", u"Β": u"B", u"Γ": u"G"}  # ...
latin_string = "".join(greek_to_latin[c] for c in greek_string)

您还可以查看 unicodedata 模块,该模块可以识别字符的类别,识别非 ASCII 标点符号。

【讨论】:

  • 使用unicode.translate 而不是以这种方式加入结果:the_string.translate(greek_to_latin)。也没有必要包含保持不变的字符:它们保持不变。
  • 当然,这也可以,尽管您必须在翻译映射中的键上使用 ord。
  • ord(u"è") 代替u"è" 没什么大不了的,但是这样可以大大加快速度。
猜你喜欢
  • 2013-06-11
  • 2011-12-28
  • 2020-12-02
  • 1970-01-01
  • 2019-12-01
  • 2019-12-10
  • 2021-09-24
  • 2017-09-28
  • 2013-01-10
相关资源
最近更新 更多