Printing Shielded Unicode in Python

>>> s = 'auszuschließen'
>>> print(s.encode('ascii', errors='xmlcharrefreplace'))
b'auszuschließen'
>>> print(str(s.encode('ascii', errors='xmlcharrefreplace'), 'ascii'))
auszuschließen

Is there a nicer way to print any line without b''?

EDIT:

I'm just trying to print escaped characters from Python, and my only problem is that Python adds "b" when I do this.

If I wanted to see the actual character in a dead end, like Windows 7, then I get the following:

Traceback (most recent call last):
  File "Mailgen.py", line 378, in <module>
    marked_copy = mark_markup(language_column, item_row)
  File "Mailgen.py", line 210, in mark_markup
    print("TP: %r" % "".join(to_print))
  File "c:\python32\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 '\u2026' in position 29: character maps to <undefined>
+5
source share
4 answers
>>> s='auszuschließen…'
>>> s
'auszuschließen…'
>>> print(s)
auszuschließen…
>>> b=s.encode('ascii','xmlcharrefreplace')
>>> b
b'auszuschlie&#223;en&#8230;'
>>> print(b)
b'auszuschlie&#223;en&#8230;'
>>> b.decode()
'auszuschlie&#223;en&#8230;'
>>> print(b.decode())
auszuschlie&#223;en&#8230;

Unicode. ascii, bytes . Python , , b . decode ; utf-8, bytes ascii, utf-8, .

+2

ascii (, repr() Python 2) :

print(ascii('auszuschließen…'))
# -> 'auszuschlie\xdfen\u2026'

:

sys.stdout.buffer.write('auszuschließen…'.encode('ascii', 'xmlcharrefreplace'))
# -> auszuschlie&#223;en&#8230;
+3

8- , . , , .

, , , , , , , - , , .

Update:

, , Unicode XML . , , b '' .

:

print(repr(s.encode('ascii', errors='xmlcharrefreplace'))[2:-1])
+1

Python 3, print(s) .

I can agree that depending on the console, it may not be able to print correctly, but I would suggest that most modern operating systems since 2006 can handle Unicode strings without any problems. I would advise you to try and see if it works.

Alternatively, you can apply encoding by placing it before any lines in the file (similar to shebang):

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

This will force the interpreter to display it as UTF-8.

0
source

All Articles