Python 2.7 character \ u2013

I have the following code:

# -*- coding: utf-8 -*-

print u"William Burges (1827–81) was an English architect and designer."

When I try to run it from cmd. I get the following message:

Traceback (most recent call last):
  File "C:\Python27\utf8.py", line 3, in <module>
    print u"William Burges (1827ŌĆō81) was an English architect and designer."
  File "C:\Python27\lib\encodings\cp775.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u2013' in position
 20: character maps to <undefined>

How can I solve this problem and get Python to read this \ u2013 character? And why Python doesn't read it with existing code, I thought utf-8 works for every character.

thanks

EDIT:

This code displays the desired result:

# -*- coding: utf-8 -*-

print unicode("William Burges (1827-81) was an English architect and designer.", "utf-8").encode("cp866")

But when I try to print a few sentences, for example:

# -*- coding: utf-8 -*-

print unicode("William Burges (1827–81) was an English architect and designer. I am here. ", "utf-8").encode("cp866")

I get the same error message:

Traceback (most recent call last):
  File "C:\Python27\utf8vs.py", line 3, in <module>
    print unicode("William Burges (1827ŌĆō81) was an English architect and desig
ner. I am here. ", "utf-8").encode("cp866")
  File "C:\Python27\lib\encodings\cp866.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u2013' in position
 20: character maps to <undefined>
+4
source share
3 answers

, , -, python ( Mac). , ; , , Windows.

, , "(1827-81)" "(1827-81)" - ? , .

. Python, Unicode Windows.

+2

wiki.python.org wiki.python.org https://wiki.python.org/moin/PrintFails, , charmap .

Setting the PYTHONIOENCODING environment variable as described above can be used to suppress the error messages. Setting to "utf-8" is not recommended as this produces an inaccurate, garbled representation of the output to the console. For best results, use your console correct default codepage and a suitable error handler other than "strict".

+1

Your line contains ndash sumbol. It is similar to ascii minus -, see Symbol No. 45 a ascii table . Replace ndash with minus because ascii cannot contain ndash. Below is a work option:

# -*- coding: utf-8 -*-

my_string = "William Burges (1827–81) was an English architect and designer."
my_string = my_string.replace("–", "-")# replace utf-8 symbol (ndash) to ascii (-)
print my_string

Exit

William Burges (1827-81) was an English architect and designer. I am here. 
0
source

All Articles