如何在字符串中转换字节UTF-8嵌入的表情符号

问题描述

我正在从WhatsApp聊天备份(chat.txt)中抓取数据。看起来像这样:

7/21/20,1:31 PM - mark: Can we look google??  
7/21/20,1:31 PM - elon: No  
7/21/20,1:31 PM - mark: Can we smile ?  
7/21/20,1:31 PM - elon: Ya?

虽然我逐行提取

with open ('chat.txt','rb') as file:
    for line in file:
        print(str(line.strip()))

我知道了:

b'7/21/20,7:37 AM - mark: Can we look google\xf0\x9f\xa4\xa9\xf0\x9f\x98\x82\xf0\x9f\x98\x82'
b'7/21/20,7:37 AM - elon: No'
b'7/21/20,1:31 PM - mark: Can we smile ?'
b'7/21/20,7:37 AM - elon: Ya\xf0\x9f\x98\x82'
  1. 我们如何摆脱b''? (我尝试过.decode('utf-8'),但是没有用)

  2. 我该如何转换

    Can we look google\xf0\x9f\xa4\xa9\xf0\x9f\x98\x82\xf0\x9f\x98\x82
    

    Can we look google???
    

解决方法

以正确的编码而非二进制模式打开文件:

groups=df.index.where(df.index.isin(['BTC','ETH']),'JPY')
print(groups)
>>>Index(['JPY','JPY','BTC','ETH','JPY'],dtype='object',name='currency')

这如何运作取决于您的执行环境。您需要一个支持打印 groups的代码点的终端机/ IDE和字体才能成功,但这不是Python问题。