我的字符串已嵌入字节字符串 - Python

My String has embedded byte string - Python

当前正在从 s3 读取数据并保存在数据帧中。

问题图片:

S3 对象以字节形式读入,但它似乎在我的字符串中,字节字符串也在那里。 无法使用 - example_string.decode().

解码字符串

另一个问题是试图在文本中找到表情符号。这些被保存为 UTF-8 并且由于被保存为字符串中的字节字符串,它添加了额外的 \ 等

我只希望没有附加字节字符串或任何组合的字符串。 任何帮助将不胜感激。

    bucket_iter = iter(bucket)
    while (True) : 
      next_val = next(bucket_iter)
      current_file = (next_val.get()['Body'].read())).decode('utf-8') 
      split_file = current_file.split(']')
      for tweet in split_file:
        a = tweet.split(',')
        if (len(a) == 10):
          a[0] = a[0][2:12]
          new_row = {'date':a[0], 'tweet':a[1], 'user':a[2], 'cashtags':a[3],'number_cashtags':a[4],'Hashtags':a[5],'number_hashtags':a[6],'quoted_tweet':a[7],'urs_present':a[8],'spam':a[9]}
          df = df.append(new_row, ignore_index=True)

s3bucket 中的一行示例

["2021-01-06 13:41:48", "Q1 2021 Earnings Estimate for The Walt Disney Company $DIS Issued By Truist Securiti https://t co/l5VSCCCgDF #stocks", "b'AmericanBanking'", "$DIS", "1", "#stocks'", "1", "False", "1", "0"]

即使这是一个字符串,它也会在字符串之前保留 'b',即使该项目是一个字符串。只需编写一小段代码,只保留引号内的内容。

def bytes_to_string(b):
  return str(b)[2:-1]

编辑:从技术上讲,您可以使用正则表达式来执行此操作,但这是一种更易读的方法(而且更短)