AtamaWarui
AtamaWarui

Reputation: 13

Open text file as input to textblob

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.

Upvotes: 1

Views: 3787

Answers (3)

backtrack
backtrack

Reputation: 8144

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

Upvotes: 2

Akash Kandpal
Akash Kandpal

Reputation: 3366

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

Upvotes: -1

SamT
SamT

Reputation: 1

You could also look into Unidecode.

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

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

Upvotes: 0

Related Questions