【问题标题】:how to decode \xe2\x80\xa6 in python?如何在 python 中解码 \xe2\x80\xa6?
【发布时间】:2020-07-05 07:10:54
【问题描述】:

对于我的文本挖掘任务,我需要先从 twitter 用 python 预处理我的数据(tweet),然后再将其放入词向量中,但是我从 twitter 上抓取数据获得的推文具有非 ASCII 字符。 如何将其解码为真实文本?

例如: 'halo kak vicki,anti cek saat ini permintaan buka isolirnya sudah dilakukan ya。 apakah jaringan indihome\xe2\x80\xa6'

这是我的代码:

import pandas as pd 
import numpy as np
import re
import ast
import nltk
from Sastrawi.StopWordRemover.StopWordRemoverFactory import StopWordRemoverFactory
from bs4 import BeautifulSoup

training = pd.read_csv('indihome2.csv')
column = training["tweet"]
uname = column.str.replace(r'@[A-Za-z0-9]+', '')
html = uname.str.replace('https://', '')
tco = html.str.replace('t.co/[a-zA-z0-9]+', '')
forb = tco.str.lstrip(':b')
fornonascii = forb.encode('latin1').decode('unicode_escape')

我已经尝试了 encode('latin1').decode('unicode_escape') 但它得到错误:'Series' object has no attribute 'encode'

【问题讨论】:

  • '\xe2\x80\xa6''…' 的 Unicode。您是否尝试过设置编码? pd.read_csv('indihome2.csv', encoding="utf8").
  • 我尝试过,但没有任何反应
  • 您查看过unidecode 包吗?
  • 我安装了它,我认为它仍然是一样的。但我想问题出在 tweepy.API 上,我应该在代码中添加“tweet_mode=extended”

标签: python python-3.x pandas decode non-ascii-characters


【解决方案1】:

试试这个,

raw_str = b'\xe2\x80\xa6'
str(raw_str, 'utf-8')

# Prints "-" 


raw_str = b'halo kak vicki, anti cek saat ini permintaan buka isolirnya sudah dilakukan ya. apakah jaringan indihome\xe2\x80\xa6'
str(raw_str, 'utf-8')

# Prints 'halo kak vicki, anti cek saat ini permintaan buka isolirnya sudah dilakukan ya. apakah jaringan indihome…'

【讨论】:

  • 如何处理整个文件?
猜你喜欢
  • 1970-01-01
  • 2018-11-04
  • 1970-01-01
  • 1970-01-01
  • 2021-02-26
  • 2017-12-05
  • 2021-12-30
  • 1970-01-01
  • 2021-10-30
相关资源
最近更新 更多