问题
Update: Issue resolved. (see comment section below.) Ultimately, the following two lines were required to transform my .csv to unicode and utilize TextBlob: row = [cell.decode('utf-8') for cell in row], and text = ' '.join(row).
Original question: I am trying to use a Python library called Textblob to analyze text from a .csv file. Error I receive when I call Textblob in my code is:
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
My code is:
#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)
I have been working through UTF/unicode issues. I'd originally had a different subject which I posed to this thread. (Since my code and the error have changed, I'm posting to a new thread.) Print statements indicate that the variable "row" is of type=str, which I thought indicated that the reader object had been transformed as required by Textblob. The source .csv file is saved as UTF-8. Can anyone provide feedback as to how I can get unblocked on this, and the flaws in my code?
Thanks so much for the help.
回答1:
So maybe you can make change as below:
row = str([cell.encode('utf-8') for cell in row])
来源:https://stackoverflow.com/questions/37150205/python-2-7-and-textblob-typeerror-the-text-argument-passed-to-init-tex