2

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.

Doug Willden
  • 21
  • 1
  • 3

2 Answers2

3

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'))
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • 1
    Using that code will just print something like "b'\xe2\x99\xa5." (Heart) How do I get the actual heart symbol to show up? I'll keep reading through that link you posted...thanks for your help so far! – Doug Willden Dec 02 '12 at 05:08
  • I've read through the link you posted and many other documents, but none of them address allowing special symbols to be printed. – Doug Willden Dec 02 '12 at 06:04
  • @DougWillden, it depends on two things: the output encoding that your console recognizes, and the font selected into that console. Ideally you want a console that recognizes UTF-8 and a font that contains the characters you want to display. – Mark Ransom Dec 02 '12 at 07:32
  • @MarkRansom, But how do I allow that to be part of my program? If i need to distribute my program, I need a way for these symbols to be integrated without the user having to edit a whole bunch of settings. All I'm using is Python 3.3 on Windows Vista, double clicking on the .py file so it runs with the command line. – Doug Willden Dec 02 '12 at 07:39
  • EDIT: command line / terminal window (whichever term suits you) – Doug Willden Dec 02 '12 at 07:50
  • 1
    @DougWillden, try pasting the characters into the console. If they don't show up then there's nothing Python can do to display them. – Mark Ransom Dec 02 '12 at 23:00
0

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

Community
  • 1
  • 1
Jan Tojnar
  • 5,306
  • 3
  • 29
  • 49
  • I tried that just now, but here's what i got: `Traceback (most recent call last): File "", line 1, in sys.stdout.buffer.write(TestText2) AttributeError: buffer` I'll read through the link now...thanks for your help so far! – Doug Willden Dec 02 '12 at 07:17
  • Yeah, sorry, that link didn't seem to help much either...more details and errors I don't understand. Thanks for your help, though. – Doug Willden Dec 02 '12 at 07:45