【问题标题】:Sentiment analysis Python TypeError: expected string or bytes-like object情绪分析 Python TypeError: expected string or bytes-like object
【发布时间】:2016-10-27 18:12:03
【问题描述】:

我正在进行情绪分析,我想在否定和标点符号之间的每个单词中添加 NOT。我正在执行以下代码:

import re


fin=open("aboveE1.txt",'r', encoding='UTF-8')

transformed = re.sub(r'\b(?:never|no|nothing|nowhere|noone|none|not|havent|hasnt|hadnt|cant|couldnt|shouldnt|wont|wouldnt|dont|doesnt|didnt|isnt|arent|aint)\b[\w\s]+[^\w\s]', 
   lambda match: re.sub(r'(\s+)(\w+)', r'\1NEG_\2', match.group(0)), 
   fin,
   flags=re.IGNORECASE)

Traceback(最近一次调用最后一次): 第 14 行,在 标志=re.IGNORECASE) 第 182 行,在 sub return _compile(pattern, flags).sub(repl, string, count) TypeError:预期的字符串或类似字节的对象

我不知道如何修复错误。你能帮帮我吗?

【问题讨论】:

    标签: python


    【解决方案1】:

    re.sub 接受一个字符串,而不是一个文件对象。文档here

    import re
    
    fin=open("aboveE1.txt",'r', encoding='UTF-8')    
    transformed = ''
    
    for line in fin:
        transformed += re.sub(r'\b(?:never|no|nothing|nowhere|noone|none|not|havent|hasnt|hadnt|cant|couldnt|shouldnt|wont|wouldnt|dont|doesnt|didnt|isnt|arent|aint)\b[\w\s]+[^\w\s]', 
        lambda match: re.sub(r'(\s+)(\w+)', r'\1NEG_\2', match.group(0)), 
        line,
        flags=re.IGNORECASE)
        # No need to append '\n' to 'transformed'
        # because the line returned via the iterator includes the '\n'
    
    fin.close()
    

    还要记住始终关闭您打开的文件。

    【讨论】:

      猜你喜欢
      • 2019-11-11
      • 1970-01-01
      • 1970-01-01
      • 2020-04-14
      • 1970-01-01
      • 2018-05-29
      • 1970-01-01
      • 2021-03-22
      • 1970-01-01
      相关资源
      最近更新 更多