Open text file as input to textblob

Question:

I am trying to use textBlob with a text file input.

All examples I found online were of input in this sense:

wiki = TextBlob("Python is a high-level, general-purpose programming language.")
wiki.tage

I tried this:

from textblob import TextBlob
file=open("1.txt");
t=file.read();
print(type(t))
bobo = TextBlob(t)
bobo.tags

The code I tried did not work.

Asked By: AtamaWarui

||

Answers:

This is a classic Unicode issue

Use

import sys  

reload(sys)  
sys.setdefaultencoding('utf8')

Then read the file

In this way you can use UTF-8 encoding/decoding format

this is outdated for Python 3.X

Answered By: backtrack

You could also look into Unidecode.

https://pypi.python.org/pypi/Unidecode

from unidecode import unidecode
...
bobo = TextBlob(unidecode(t))

Answered By: SamT

For Python3 guys:

import sys  
from importlib import reload
reload(sys)  
sys.getdefaultencoding() # use this for Python3
from textblob import TextBlob
url ='filename.txt'
file=open(url)
t=file.read()
print(type(t))
bobo = TextBlob(t)
bobo.tags
Answered By: Akash Kandpal
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.