简体   繁体   中英

Python 2 Print emoji in a sentence as rectangles

I have this 😭👏🏻 ( loudly crying face and clapping hand emoji character) in a string.txt file (encoded in utf-8 ).

I am trying to print it out into the default python IDLE, in a sentence.

with open('string.txt','r') as f:
    string = f.read()

The code:

>>> string
'\xf0\x9f\x98\xad\xf0\x9f\x91\x8f\xf0\x9f\x8f\xbb'

>>> print string
ð゚リᆳð゚ムマð゚マᄏ

>>> print string.decode('utf-8')
😭👏🏻    # <-- this is the output I want in a middle of sentence

That's the output I want (rectangles). The tricky part is that I want them in middle of a sentence. So:

>>> print 'The string is: {}!'.format(string.decode('utf-8')) # will get error
Traceback (most recent call last):
  File "<pyshell#81>", line 1, in <module>
    print 'The string is: {}!'.format(string.decode('utf-8'))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)

Got an error. But if I don't decode it, it works:

>>> print 'The string is: {}!'.format(string)
The string is: ð゚リᆳð゚ムマð゚マᄏ!

It did not raise any error, but I don't want this output. I want the rectangles.

How should I solve this issue so it will behave like this:

>>> print 'The string is: {}!'.format(magical_string)
The string is: 😭👏🏻!

Preferred to not use any 3rd party library.

EDIT:

My Operating System: Windows 7 (preferred solution for all Windows 7-10)

Python: 2.7

I think it's a setting of your IDE, and not really a python issue.

When I save the first line of your question into a txt file and read it:

Copied from terminal:

>>> open('test.txt').read()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\joost\Desktop\pythontests\venv\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 19: character maps to <undefined>
>>> open('test.txt', encoding='utf-8').read()
'I have this 😭👏🏻 (loudly crying\n'
>>>

As a picture:

在此处输入图片说明

Perhaps specify your encoding when opening the file?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM