如何在python 3中打印各种字母的字符?

时间:2014-01-16 21:55:22

标签: python encoding cyrillic

我正在尝试使用西里尔字母和拉丁字母与中欧字符,但我无法打印西里尔字符。请看下面的示例代码。

# -*- coding: utf-8 -*-

print("ň")
print("ф")

我已经能够输出“ň”,一旦我在Python.sublime-settings中设置了“encoding”:“cp1250”,但遗憾的是我还没有找到任何显示西里尔字符的方法。

感谢您的帮助。

------------------------编辑---------------------- ----

同时我把代码放在一起,在Ubuntu 13.04环境中工作,但在Win 7中抛出异常。

例外:

Traceback (most recent call last):
  File "C:\Users\branislavs\docs\personal\scripts\playground.py", line 6, in <module>
    for line in data:
  File "C:\Python34\lib\encodings\cp1250.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 51: character maps to <undefined>

环境: 赢7 Python 3.4, Sublime Text 2, 我试图在Windows控制台上输出它。

赢得什么才能让它发挥作用?

我也粘贴了代码:

import sys

char_mapping = ("абвгґдезіийклмнопрстуфъыьцчжшАБВГҐДЕЗIИЙКЛМНОПРСТУФЪЫЬЦЧЖШ",
                "abvhgdeziyjklmnoprstuf'ŷ'cčžšABVHGDEZIYJKLMNOPRSTUF'Ŷ'cČŽŠ")


syllable_mapping = {
    "special": { #di ti ni li, da ta na la, cja, cji, sja, sji, rja, rji
        "ďi": "дї",
        "Ďi": "Дї",
        "ťi": "тї",
        "Ťi": "Тї",
        "ňi": "нї",
        "Ňi": "Нї",
        "ľi": "лї",
        "Ľi": "Лї", 

        "ďa": "дя",
        "Ďa": "Дя",
        "ťa": "тя",
        "Ťa": "Тя",
        "ňa": "ня",
        "Ňa": "Hя",
        "ľa": "ля",
        "Ľa": "Ля",

        "c'a": "ця",
        "c'a": "Ця",
        "c'i": "цї",
        "C'i": "Цї",
        "c'o": "цё", 
        "C'o": "Цё", 

        "s'a": "ся",
        "S'a": "Ся",
        "s'i": "сї",
        "S'i": "Сї",
        "s'o": "сё", 
        "S'o": "Сё", 

        "r'a": "ря",
        "R'a": "Ря",
        "r'i": "рї",
        "R'i": "Рї",
        "r'o": "рё", 
        "R'o": "Рё", 

        "z'a": "зя", 
        "Z'a": "Зя", 
        "z'i": "зї",
        "Z'i": "Зї",
        "z'o": "зё", 
        "Z'o": "Зё", 
    },

    "carons": {
        "ď": "дь",
        "Ď": "Дь",
        "ť": "ть",
        "Ť": "Ть",
        "ň": "нь",
        "Ň": "Нь",
        "ľ": "ль",
        "Ľ": "Ль",

    },

    "basic"  : {
        "ja": "я",
        "Ja": "Я",
        "ju": "ю",
        "Ju": "Ю",
        "je": "є",        
        "Je": "Є",        
        "ch": "х",
        "Ch": "X",
        "'o": "ё",
        "'O": "Ë",
        "x": "кc",
        "X": "Кc",
        "šč": "щ",
        "Šč": "Щ",
        "ji": "ї",

        "c'" : "ць",
        "C'" : "Ць",
        "s'" : "сь",
        "S'" : "Сь",
        "r'" : "рь",
        "R'" : "Рь",        
        "z'" : "зь",
        "Z'" : "Зь",
    }

}



tr_azb_lat = {ord(a):ord(b) for a, b in zip(*char_mapping)}
tr_lat_azb = {ord(b):ord(a) for a, b in zip(*char_mapping)}

def map_syllables_azb_lat(string, mapping_option):
    for rule in syllable_mapping[mapping_option]:
        string = string.replace(syllable_mapping[mapping_option][rule], rule)
    return string

def translit_azb_lat(string):
    string = map_syllables_azb_lat(string, 'special')
    string = map_syllables_azb_lat(string, 'carons')
    string = map_syllables_azb_lat(string, 'basic')
    return string.translate(tr_azb_lat).encode('utf-8').decode(sys.stdout.encoding)

def map_syllables_lat_azb(string, mapping_option):
    for rule in syllable_mapping[mapping_option]:
        string = string.replace(rule, syllable_mapping[mapping_option][rule])
    return string


def translit_lat_azb(string):
    string = map_syllables_lat_azb(string, 'special')
    string = map_syllables_lat_azb(string, 'carons')
    string = map_syllables_lat_azb(string, 'basic')
    return string.translate(tr_lat_azb).encode('utf-8').decode(sys.stdout.encoding)

0 个答案:

没有答案