在解码pcap文件并读取时,这部分出现了错误,如果不加utf-8会报gbk错误,加了以后
File "E:\PYTHON\lib\codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd4 in position 0: invalid continuation byte
请求帮助我解决这个问题
def decodeTxt(filePath):
times = []
length = []
packetNum = 0
with open(filePath, "r",encoding="utf-8") as file:
for dada in file:
dada = dada.split(" ")
times.append(float(dada[0]))
length.append(int(dada[1]))
packetNum += 1
return times, length
这个错误说明文件中包含了无法用utf-8编码解码的字节。你可以尝试使用其他编码方式打开文件,比如gbk:
with open(filePath, "r", encoding="gbk") as file:
如果这个方式不行,说明文件的编码不是gbk或utf-8,你可以尝试使用其他编码方式打开文件,或者使用二进制方式打开文件并尝试解码。比如:
with open(filePath, "rb") as file:
data = file.read()
text = data.decode("xxx") # 使用不同的编码方式进行解码
lines = text.split("\n")
for line in lines:
# 处理每一行数据