fread python有没有_将C的fread转换为python,没有得到预期的输出

我正在把一个小程序从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中实现的背景,还是我做错了?在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值