前言

今天在用dpkt读.pcap文件,发现咋一直在报错,遂觉是读取数据的问题。在stackoverflow找到了答案,需要在open的时候加一个参数rb,read+byte,就解决了。

正文

Before:

f = open('/content/drive/MyDrive/07 - Datasets/IoT_Sentinel/captures_IoT_Sentinel/captures_IoT-Sentinel/EdnetGateway/Setup-A-1-STA.pcap')
pcap = dpkt.pcap.Reader(f)

After (it works):

f = open('/content/drive/MyDrive/07 - Datasets/IoT_Sentinel/captures_IoT_Sentinel/captures_IoT-Sentinel/EdnetGateway/Setup-A-1-STA.pcap', 'rb')
pcap = dpkt.pcap.Reader(f)

See the definition for open with arguments rb:

In Python, opening a file in 'rb' mode means that the file is being opened for reading in binary mode. The 'r' stands for "read mode," which allows the file to be read from but not written to, and the 'b' stands for "binary mode," which specifies that the file is being read as a binary file, rather than a text file.

总结

做个记录,就是酱紫~!

参考

[1] UnicodeDecodeError, invalid continuation byte

Q.E.D.


立志做一个有趣的碳水化合物