Python 2.7 and Textblob - TypeError: The `text` argument passed to `__init__(text)` must be a string, not <type 'list'>

Python 2.7 and Textblob - TypeError: The `text` argument passed to `__init__(text)` must be a string, not <type 'list'>

更新:问题已解决。(请参阅下面的评论部分。)最终,需要以下两行才能将我的 .csv 转换为 unicode 并利用 TextBlob:row = [cell.decode('utf-8') for cell in row], and text = ' '.join(row).

原问题: 我正在尝试使用名为 Textblob 的 Python 库来分析 .csv 文件中的文本。我在代码中调用 Textblob 时收到的错误是:

Traceback (most recent call last): File "C:\Users\Marcus\Documents\Blog\Python\Scripts\Brooks\textblob_sentiment.py", line 30, in blob = TextBlob(row) File "C:\Python27\lib\site-packages\textblob\blob.py", line 344, in init 'must be a string, not {0}'.format(type(text)))TypeError: The text argument passed to __init__(text) must be a string, not

我的代码是:

#from __future__ import division, unicode_literals #(This was recommended for Python 2.x, but didn't help in my case.)

#-*- coding: utf-8 -*-
import csv
from textblob import TextBlob
with open(u'items.csv', 'rb') as scrape_file:
reader = csv.reader(scrape_file, delimiter=',', quotechar='"')
for row in reader:
    row = [unicode(cell, 'utf-8') for cell in row]
    print row
    blob = TextBlob(row)
    print type(blob)

我一直在解决 UTF/unicode 个问题。我最初有一个不同的主题 this thread。 (因为我的代码和错误已经改变,我发布到一个新线程。)打印语句表明变量 "row" 是类型=str,我认为这表明 reader 对象有根据 Textblob 的要求进行了转换。源 .csv 文件保存为 UTF-8。任何人都可以就我如何才能解除封锁以及我的代码中的缺陷提供反馈吗?
非常感谢您的帮助。

所以也许您可以进行如下更改:

row = str([cell.encode('utf-8') for cell in row])