将 C 的 fread 转换为 python,没有得到预期的输出
Converting C's fread to python, not getting expected output
我正在将一个小程序从 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
中的文件并像这样读取它。
TEXT_SHORT = 64;
fread(&(record->id), sizeof(int), 1, fp);
fread(&(record->name[0]), sizeof(char), TEXT_SHORT, fp);
fread(&(record->location[0]), sizeof(char), TEXT_SHORT, fp);
printf("%06d\n", record->id);
printf("%s\n", record->name);
printf("%s\n", record->location);
然后在打印值时,我得到这个:
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 中实现的背景,还是我做错了?
虽然您在 C 和 Python 中读取一个 64 字节的块,但 Python 没有 \x00
这样的东西作为字符串终止符。因此,虽然 C 中的 printf
将打印到第一个 [=14=]
,但 Python 将打印整个缓冲区,包括尾随垃圾。
只需在 [=14=]
拆分字符串,只保留第一部分:
name = name.split(b"[=10=]", 1)[0]
location = name.split(b"[=10=]", 1)[0]
顺便说一句,您可以在一行中检索 3 个元素:
id, name, location = struct.unpack("i64s64s", file.read(132))
name = name.split(b"[=11=]", 1)[0]
location = name.split(b"[=11=]", 1)[0]
我正在将一个小程序从 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
中的文件并像这样读取它。
TEXT_SHORT = 64;
fread(&(record->id), sizeof(int), 1, fp);
fread(&(record->name[0]), sizeof(char), TEXT_SHORT, fp);
fread(&(record->location[0]), sizeof(char), TEXT_SHORT, fp);
printf("%06d\n", record->id);
printf("%s\n", record->name);
printf("%s\n", record->location);
然后在打印值时,我得到这个:
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 中实现的背景,还是我做错了?
虽然您在 C 和 Python 中读取一个 64 字节的块,但 Python 没有 \x00
这样的东西作为字符串终止符。因此,虽然 C 中的 printf
将打印到第一个 [=14=]
,但 Python 将打印整个缓冲区,包括尾随垃圾。
只需在 [=14=]
拆分字符串,只保留第一部分:
name = name.split(b"[=10=]", 1)[0]
location = name.split(b"[=10=]", 1)[0]
顺便说一句,您可以在一行中检索 3 个元素:
id, name, location = struct.unpack("i64s64s", file.read(132))
name = name.split(b"[=11=]", 1)[0]
location = name.split(b"[=11=]", 1)[0]