0

I used Python2.7 to do this but I always got the same error when I try to insert the '€' symbol in my code. My OS system is windows 10.

When I run this code:

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('\\------------------------------------/'))

I got this:

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

but when I add the $ symbol I got:

╔════════════════════════════════════╗
║                                    ║
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>

What I did wrong?

Federico Lolli
  • 163
  • 1
  • 12
  • 1
    Your *Windows console* can't handle the `\u20ac` character you are trying to print, because the CP1252 characterset has no mapping for that character. There is nothing wrong with your code. – Martijn Pieters Oct 31 '16 at 09:50

1 Answers1

0
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('|                $                    |'))

try it again

kotepillar
  • 114
  • 7
  • 1
    I got this: ÔòöÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòù ÔòæÔòæ ÔòæÔòæ ÔòÜÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòÉÔòØ – Federico Lolli Oct 31 '16 at 10:07
  • As Martijn Pieters Said, your code does not have any problem, you can right click on your windows console, and reset your characterset. – kotepillar Oct 31 '16 at 10:28