【问题标题】:TypeError: expected string or bytes-like object I am getting this errorTypeError:预期的字符串或类似字节的对象我收到此错误
【发布时间】:2021-09-19 12:21:59
【问题描述】:

这是我遇到错误的代码。我有一个 excel 表,其中 2 列 A 列包含乌尔都语文本,B 列包含文本类别 True 或 False

counter = collections.Counter()
maxlen = 0
c=0
df=pd.read_excel(INPUT_FILE)
df.columns=['Text','label']
label=df['label']
sent=df['Text']
# print (sent)
content=df
for index, row in df.iterrows():
#   print(row['Text'], row['label'])
    words = [x.lower() for x in nltk.word_tokenize(row['Text'])] # here i am getting error 
    if len(words) > maxlen:
        maxlen = len(words)
        c=c+1
    for word in words:
        counter[word] += 1
print (len(counter))
print (maxlen)

我已经尝试了不同的解决方案,但都是徒劳的。请根据我的代码和我的问题向我推荐一些代码或解决方案。我使用的是乌尔都语,这可能是个问题。

【问题讨论】:

  • 你能告诉我们这个错误发生在哪里吗?您还可以正确格式化您帖子中的代码吗?
  • 发布完整的回溯消息而不是摘要。它包含有价值的信息。
  • Victor Douet 它出现在 for 循环之后,从“words = [x”开始的行。这个。
  • 再次,编辑您的问题以包含 full 回溯和 full 错误消息,而不是重新键入或编辑的版本。忽略您认为不重要的内容会限制我们对问题的理解。
  • 如果您怀疑问题出在乌尔都语...为什么不尝试其他方法?

标签: python nlp


【解决方案1】:

"word_tokenize""string" 类型作为输入,而您传递的对象似乎是不同的 type

参考这个:

https://www.nltk.org/api/nltk.tokenize.html

word_tokenize(s)[source]¶
Tokenize a string to split off punctuation other than periods

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    • 2017-03-14
    • 1970-01-01
    • 2018-10-17
    相关资源
    最近更新 更多