pkr*_*iak 6 python nltk python-import python-2.7
我尝试导入nltk模块时显示以下错误消息
其实,我的0xb3(?)人物在我的用户名,但让我困扰的是,其他的模块,如re,codecs等成功导入.
是否有可能在Python端解决它(不改变系统范围内的用户名)?
File "C:\Python27\lib\ntpath.py", line 310, in expanduser
return userhome + path[i:]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb3 in position 13: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
由于 ntpath.py文件中没有 unicode username 的编码,因此您需要在expanduser(path)函数中添加以下命令ntpath.py:
if isinstance(path, unicode):
userhome = unicode(userhome,'unicode-escape').encode('utf8')
Run Code Online (Sandbox Code Playgroud)
所以expanduser函数必须如下所示:
def expanduser(path):
"""Expand ~ and ~user constructs.
If user or $HOME is unknown, do nothing."""
if isinstance(path, bytes):
tilde = b'~'
else:
tilde = '~'
if not path.startswith(tilde):
return path
i, n = 1, len(path)
while i < n and path[i] not in _get_bothseps(path):
i += 1
if 'HOME' in os.environ:
userhome = os.environ['HOME']
elif 'USERPROFILE' in os.environ:
userhome = os.environ['USERPROFILE']
elif not 'HOMEPATH' in os.environ:
return path
else:
try:
drive = os.environ['HOMEDRIVE']
except KeyError:
drive = ''
userhome = join(drive, os.environ['HOMEPATH'])
if isinstance(path, bytes):
userhome = userhome.encode(sys.getfilesystemencoding())
if isinstance(path, unicode):
userhome = unicode(userhome,'unicode-escape').encode('utf8')
if i != 1: #~user
userhome = join(dirname(userhome), path[1:i])
return userhome + path[i:]
Run Code Online (Sandbox Code Playgroud)