Python:€符号的UTF-8编码

费德里科·洛里(Federico Lolli)

我使用Python2.7来执行此操作,但是当我尝试在代码中插入“€”符号时,总是会遇到相同的错误。我的操作系统是Windows 10。

当我运行此代码时:

dic = {
'\\' : b'\xe2\x95\x9a',
'-'  : b'\xe2\x95\x90',
'/'  : b'\xe2\x95\x9d',
'|'  : b'\xe2\x95\x91',
'+'  : b'\xe2\x95\x94',
'%'  : b'\xe2\x95\x97',
'$'  : b'\xe2\x82\xac',
}
def decode(x):
    return (''.join(dic.get(i, i.encode('utf-8')).decode('utf-8') for i in x))
print(decode('+------------------------------------%'))
print(decode('|                                    |'))
print(decode('|                                 |'))
print(decode('\\------------------------------------/'))

我懂了:

╔════════════════════════════════════╗
║                                    ║
║                                    ║
╚════════════════════════════════════╝

但是当我添加$符号时,我得到了:

╔════════════════════════════════════╗
║                                    ║
Traceback (most recent call last):
  File "test.py", line 14, in <module>
    print(decode('|                $                    |'))
  File "C:\Python27\lib\encodings\cp850.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u20ac' in position 17: character maps to <undefined>

我做错了什么?

科特彼勒
dic = {
'\\' : b'\xe2\x95\x9a',
'-'  : b'\xe2\x95\x90',
'/'  : b'\xe2\x95\x9d',
'|'  : b'\xe2\x95\x91',
'+'  : b'\xe2\x95\x94',
'%'  : b'\xe2\x95\x97',
'$'  : b'\xe2\x82\xac',
}
def decode(x):
    return (''.join(dic.get(i, '') for i in x))
print(decode('+------------------------------------%'))
print(decode('|                                    |'))
print(decode('|                                 |'))
print(decode('|                $                    |'))

再试一次

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章