[英]UnicodeDecodeError: 'utf-8' codec can't decode byte 0x95 in position 3: invalid start byte (Python) socket programming
[英]UnicodeDecodeError: 'utf-8' codec can't decode byte 0x95 in position 1072: invalid start byte
我正在嘗試使用 python 打開並讀取 csv 文件,但我不斷收到此錯誤。
Traceback (most recent call last):
File "openfile.py", line 7 in <module>
data = csvfile("1.csv")
File "openfile.py", line 4, in csvfile
data = np.loadtxt(filename, delimiter = ",", skiprows =9)
File "/Users/ZEN/anaconda/lib/python3.6/site-packages/numpy/lib/npyio.py", line 880, in loadtxt
next(fh)
File "/Users/ZEN/anaconda/lib/python3.6/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x95 in position 1072: invalid start byte
我不知道為什么我不能正常運行這段代碼。 我在 Mac 上使用 python 3.6.0 和 Anaconda 4.3.1(x86_64)。 我最近從 2.x 升級到 python 3.x 並下載了 numpy。 這是我試圖運行的代碼:
import numpy as np
def csvfile(filename):
data = np.loadtxt(filename, delimiter = ",", skiprows =9)
return data
data = csvfile("1.csv")
print (data)
如果有人可以幫助我,那就太好了!
我不確定您使用的究竟是什么編碼。
如果你的數據包含中文或一些特殊字符,試試這個
data = np.loadtxt(path,encoding="gbk", delimiter=",",dtype="str")
如果沒有,一般來說,使用
data = np.loadtxt(path,encoding="utf8", delimiter=",",dtype="str")
可以解決問題。
了解您的文件使用的編碼非常重要。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.