Search code examples
pythonreadfiletextblob

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.


Solution

  • 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