python 文本框不能放表情_Python-如何打开其中包含表情符号的文本文件

I´m trying to do the simplest thing, open a file, read and close it in python. Simple. Well this is the code:

name_file = open("Forever.txt", encoding='UTF-8')

data = name_file.read()

name_file.close()

print (data)

I know that this texts has emojis in it like hearts, etc. The thing is that this emojis are not in there unicode syntax like U+2600 , they are placed as little images. I think the following error is because of this little images:

return codecs.charmap_encode(input,self.errors,encoding_table)[0]

UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f681' in

position 2333: character maps to

I tried the following, without specifyng encoding:

name_file = open("Forever.txt")

And the error changed to this:

UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 2303: character maps to

No idea why is this happening.

Maybe one solution would be to save in a variable everything that is test and deleting the rest...mmm

Any help will be really appreciated

解决方案

You are getting a UnicodeEncodeError, likely from your print statement. The file is being read and interpreted correctly, but you can only print characters that your console encoding and font actually support. The error indicates the character isn't supported in the current encoding.

For example:

Python 3.3.5 (v3.3.5:62cf4e77f785, Mar 9 2014, 10:35:05) [MSC v.1600 64 bit (AMD64)] on win32

Type "help", "copyright", "credits" or "license" for more information.

>>> print('\U0001F681')

Traceback (most recent call last):

File "", line 1, in

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 '\U0001f681' in position 0: character maps to

But print a character the terminal encoding supports, and it works:

>>> print('\U000000E0')

à

My console encoding was cp437, but if I use a Python IDE that supports UTF-8 encoding, then it works:

>>> print('\U0001f681')

🚁

You may or may not see the character correctly. You need to be using a font that supports the character; otherwise, you get some default replacement character.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值