【问题标题】:re.sub : How to solve TypeError: expected string or bytes-like objectre.sub : 如何解决 TypeError: expected string or bytes-like object
【发布时间】:2020-07-15 14:05:07
【问题描述】:

我有一个名为tweet 的dataframe,具有以下类型:

                        Id                                               Text
0      1281015183687720961  @AngelaRuchTruck has @BubbaWallace beat, by fa...
1      1281015160803667968  I’m an old, white male. I marched in the 60s a...
2      1281014374744891392  This is me and I am saying #EnoughIsEnoughNS L...
3      1281014363193819139  The Ultimate Fighter Finale! Join in on the fu...
4      1281014339433095169                       This #blm $hit is about done
...                    ...                                                ...
12529  1279207822207725569  First thing I see, getting here #BLM #BLMDC #B...
12530  1279206857253543936  So here’s a thought for all of you #BLM people...
12531  1279206802035539969  #campingworld #Hamilton #BreakTheSilenceForSus...
12532  1279205845474127872  #Day 3.168 . . #artmenow #drawmenow #nodapl #n...
12533  1279205399535792128  Oh but wait ....... Breonna Taylor! #BreonnaTa...

我正在尝试使用以下代码清理文本 tweet['Text']

tweet['cleaned_text'] = re.sub(r"(?:\@RT|http?\://|https?\://|www)\S+", "", tweet['Text'])

tweet['cleaned_text']= re.sub(r'^RT[\s]+', '', tweet['cleaned_text']))

但我得到这个错误:

~\AppData\Local\Continuum\anaconda3\lib\re.py in sub(pattern, repl, string, count, flags)
    190     a callable, it's passed the Match object and must return
    191     a replacement string to be used."""
--> 192     return _compile(pattern, flags).sub(repl, string, count)
    193 
    194 def subn(pattern, repl, string, count=0, flags=0):

TypeError: expected string or bytes-like object

建议的答案是使用以下代码:

cleaned = []
txt = list(tweet['Text'])
for i  in txt:
    cleaned.append(re.sub(r"(?:\@RT|http?\://|https?\://|www)\S+", "", i))
tweet['cleaned_text'] = cleaned

代码运行良好。但是,tweet['cleaned_text'] 仍然不是字符串。例如当我使用以下代码时:

Blobtweet = TextBlob(tweet["cleaned_text"]) 

我收到此错误

~\AppData\Local\Continuum\anaconda3\lib\site-packages\textblob\blob.py in __init__(self, text, tokenizer, pos_tagger, np_extractor, analyzer, parser, classifier, clean_html)
    368         if not isinstance(text, basestring):
    369             raise TypeError('The `text` argument passed to `__init__(text)` '
--> 370                             'must be a string, not {0}'.format(type(text)))
    371         if clean_html:
    372             raise NotImplementedError("clean_html has been deprecated. "

TypeError: The `text` argument passed to `__init__(text)` must be a string, not <class 'pandas.core.series.Series'>

########### 或

text=tweet['cleaned_text']
text = text.lower()  
tokens = tokenizer.tokenize(text)

我收到以下错误:

AttributeError: 'Series' object has no attribute 'lower'

当我有一个字符串时,所有这些示例都可以正常工作

【问题讨论】:

    标签: python-3.x string dataframe python-re


    【解决方案1】:

    tweet['cleaned_text'] 返回一列,而不是字符串,您必须遍历列的每个元素。

    cleaned = []
    txt = list(tweet['Text'])
    for i  in txt:
        t = re.sub(r"(?:\@RT|http?\://|https?\://|www)\S+", "", i)
        cleaned.append(re.sub(r'^RT[\s]+', '', t))
    tweet['cleaned_text'] = cleaned
    

    【讨论】:

    • 谢谢,您的代码对我来说很好用。但是,tweet['cleaned_text'] = cleaned 仍然不是字符串,我无法进一步处理它以进行情绪分析TextBlob 或类似的东西。我收到类似于上述TypeError: expected string or bytes-like object 的错误
    • 当您使用我建议的编辑运行代码时,您能否发布新错误。
    • 请查看已编辑的问题以及您的建议答案和错误。
    • 使用text = [i.lower() for i in text]
    • 谢谢 Roshin。您的代码适用于 text = text.lower() 的情况
    猜你喜欢
    • 2019-11-11
    • 2016-10-27
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    相关资源
    最近更新 更多