Doug Willden
Doug Willden

Reputation: 21

Python 3.3: printing special symbols like ♥ and ❦ when running through command line

I've found quite a bit of special symbols like ♥ and ❦ that I want to use for a program with Python 3.3, and the programs work just fine in IDLE, but when I run it using the command line it immediately crashes, giving an error about characters not being recognized:

Traceback (most recent call last):
    File "C:python33\meh.py", line 1, in <module>
        print("\u2665")
    File "C:\python33\lib\encodings\cp437.py", line 19, in encode
        return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character 'u\u2665' in position 0: character maps to <undefined>

How would one go about getting these symbols to map to something else other than <undefined> so it can show in the command line?

I assume I could try adding to python's cp437.py (figured that from the error), but I'm not sure how do get the hexidecimal/decimal/whatever for each symbol and where to put it.

Example for LATIN CAPITAL LETTER C WITH CEDILLA:

--decoding map key and value: 0x0080: 0x00c7, # LATIN CAPITAL LETTER C WITH CEDILLA

--decoding table string: '\xc7' # 0x0080 -> LATIN CAPITAL LETTER C WITH CEDILLA

--encoding map key and value: 0x00c7: 0x0080, # LATIN CAPITAL LETTER C WITH CEDILLA

I can't tell if all symbols need all three (let alone what each do). some have the decoding map and table, but others just have the decoding table and the encoding map instead.

Upvotes: 2

Views: 5939

Answers (2)

Jan Tojnar
Jan Tojnar

Reputation: 5524

Personally, it works like a charm for me on Ubuntu, which uses unicode by default but you can try printing the text using

import sys
TestText = "Test - āĀēĒčČ..šŠūŪžŽ" # this NOT utf-8...it is a Unicode string in Python 3.X.
TestText2 = TestText.encode('utf8')
sys.stdout.buffer.write(TestText2)

From https://stackoverflow.com/a/3603160/160386

Upvotes: 0

Ned Batchelder
Ned Batchelder

Reputation: 375574

You have to properly encode the characters for your terminal. Pragmatic Unicode, or, How Do I Stop The Pain has details.

For example:

print(my_text.encode('utf8'))

Upvotes: 3

Related Questions