Reputation: 25
I am trying to print the currency symbol using python. I am able to print some currency symbols by normalizing the unicode character.Because in python 2.7, in the character map, I can see mappings only for some unicode characters and not for EURO symbol that is '\u20ac'.
To print EURO symbol, do we need to include the unicode characters the character map python file?
OR is there any other way which we can use to print the EURO symbol?
I use the below code and I am getting below error.
Output:
Yen
¥
Euro
Traceback (most recent call last):
File ".\new-test.py", line 8, in <module>
print list1
File "C:\Python27\lib\encodings\cp437.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u20ac' in position 0: character maps to <undefined>
Code:
from __future__ import unicode_literals
import unicodedata
list = ['Yen','\u00a5','Euro',u'\u20ac']
for character in list:
list1 = (unicodedata.normalize("NFKD", character)).strip()
print list1
Upvotes: 1
Views: 3343
Reputation: 177406
Your Windows command prompt is configured to use code page 437 (cp437
) and the Euro symbol is not defined in that encoding. You can change the code page to 1252 which supports the character:
C:\>py -2
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print u'\u20ac'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\dev\Python27\lib\encodings\cp437.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u20ac' in position 0: character maps to <undefined>
>>> ^Z
C:\>chcp 1252
Active code page: 1252
C:\>py -2
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print u'\u20ac'
€
A better alternative is to switch to Python 3.6 or later, which uses Windows Unicode APIs to write directly to the console, bypassing encoding issues:
C:\>py -3
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print('\u20ac')
€
Upvotes: 1