AllenInWood's Blog

WritingAndCoding

  • Home
  • project
  • Categories
  • About
  • Archives
  • Tags
  • 公益404

nltk UnicodeDecoderError

Posted on 2016-11-15   |   In code   |   0 comments   |   visitors

Python version: 2.7
Windows version: Windows 7 64-bit
Language of the system: Chinese
After downloading the nltk package, here comes a problem when importing.
Here is my code:

>>>import nltk

However, I get the following error:

Traceback (most recent call last):
  File "C:\Users\Александр\Desktop\TextBlob.py", line 1, in <module>
    import textblob
  File "C:\Python27\lib\site-packages\textblob\__init__.py", line 9, in <module>
   from .blob import TextBlob, Word, Sentence, Blobber, WordList
   File "C:\Python27\lib\site-packages\textblob\blob.py", line 28, in <module>
    import nltk
  File "C:\Python27\lib\site-packages\nltk\__init__.py", line 128, in <module>
    from nltk.chunk import *
  File "C:\Python27\lib\site-packages\nltk\chunk\__init__.py", line 155, in <module>
   from nltk.data import load
  File "C:\Python27\lib\site-packages\nltk\data.py", line 77, in <module>
    if 'APPENGINE_RUNTIME' not in os.environ and os.path.expanduser('~/') != '~/':
  File "C:\Python27\lib\ntpath.py", line 311, in expanduser
    return userhome + path[i:]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc0 in position 9: ordinal not in range(128)
>>>

Just as illustrated above, the problem is related to language coding problems of ntpath.py
From Google and Stackoverflow, a solution works fine.
I inserted following part into ntpath.py:

reload(sys)
sys.setdefaultencoding('Cp1252')

And the code in this file has been changed to:

import os
import sys
import stat
import genericpath
import warnings

#change
reload(sys)
sys.setdefaultencoding('Cp1252')

Finally, it works well.

夜雨

Posted on 2016-11-05   |   In essay   |   0 comments   |   visitors

夜雨


文:小疏(allen) | 图:网络

夜雨是让人灰心的。

Read more »

Hello World

Posted on 2016-11-01   |   In code   |   0 comments   |   visitors

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

1…34
AllenInWood

AllenInWood

imagination

18 posts
3 categories
30 tags
GitHub
© 2016 - 2019 AllenInWood