我正在把一个小程序从C转换成Python,但是我在读取文件时遇到了问题。它是一个.dat文件,数据为十六进制格式。这是我要读取的头132个字节2400 0000 4c61 7a61 726f 2053 756e 6965
7200 ffff 0000 0000 7261 6a70 6f6f 7420
6279 776f 726b 2069 7363 6869 6f70 7562
6963 2073 6872 6f76 6574 6964 6520 6469
7373 7561 5275 746c 616e 642c 5665 726d
6f6e 742c 0d00 0000 7000 0000 0000 0000
0000 0000 0000 0000 4000 0000 0000 0000
ffff ffff 656e 2073 6f76 6572 6f62 6564
6965 6e74
读取它的C代码打开fp中的文件,并按如下方式读取它。在
^{pr2}$
当打印值时,我得到:36
Lazaro Sunier
Rutland,Vermont,
为了将此功能转换为Python,我编写了以下代码:def read_file(file):
id = struct.unpack('i', file.read(4))[0]
name = ''.join(struct.unpack('c'*64, file.read(64)))
location = ''.join(struct.unpack('c'*64, file.read(64)))
print(id)
print(name)
print(location)
然后我得到这个输出36
Lazaro Sunier��rajpoot bywork ischiopubic shrovetide dissua
p@����en soverobedient
我已经为此挣扎了一段时间,不知道为什么会这样。fread()所做的是我需要在Python中实现的背景,还是我做错了?在