Traceback (most recent call last):
File "Final_3.py", line 42, in <module>
np.savetxt("table.csv", output_arr, fmt='%s' , delimiter=",")
File "/usr/local/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 1381, in savetxt
fh.write(v)
File "/usr/local/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 1291, in write_normal
self.fh.write(asunicode(v))
File "/usr/local/lib/python2.7/dist-packages/numpy/compat/py3k.py", line 70, in asunicode
return str(s).decode('ascii')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xce in position 22: ordinal not in range(128)
我也试过了
重装(SYS)
sys.setdefaultencoding('utf8')在我的代码中,但它仍然无济于事。
答案 0 :(得分:0)
当Python尝试将Unicode转换为字节时,会发生UnicodeDecodeError
。 Python 2在某些情况下会隐式执行此操作,而Python 3则不会。您的错误消息表明您正在使用Python 2.7。
如果你一直使用Unicode字符串,你的问题很可能就会消失:
np.savetxt(u"table.csv", output_arr, fmt=u'%s' , delimiter=u",")
答案 1 :(得分:0)
我在Py3上安装了numpy,因此无法直接测试你的代码。但是错误回溯看起来像是使用版本1.14,和我一样。所以这应该是相关的:
创建一个包含0xce
个问题字符的数组:
In [553]: arr = np.array(['abc','def',chr(0xce)*3])
In [554]: arr
Out[554]: array(['abc', 'def', 'ÎÎÎ'], dtype='<U3')
尝试使用ascii
编码保存,会发出类似的消息:
In [555]: np.savetxt('test.txt',arr,fmt='%s',encoding='ascii')
---------------------------------------------------------------------------
UnicodeEncodeError Traceback (most recent call last)
<ipython-input-555-c52f408b633c> in <module>()
----> 1 np.savetxt('test.txt',arr,fmt='%s',encoding='ascii')
/usr/local/lib/python3.5/dist-packages/numpy/lib/npyio.py in savetxt(fname, X, fmt, delimiter, newline, header, footer, comments, encoding)
1374 "format specifier ('%s')"
1375 % (str(X.dtype), format))
-> 1376 fh.write(v)
1377
1378 if len(footer) > 0:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
更改编码有效:
In [556]: np.savetxt('test.txt',arr,fmt='%s',encoding='utf8')
In [557]: cat test.txt
abc
def
ÎÎÎ