简体   繁体   English

使用Python从文件读取整数

[英]Reading integers from file using Python

I´ve a "binary" file with variable size record. 我有一个带有可变大小记录的“二进制”文件。 Each record is composed of an amount of little endians 2 byte-sized integer numbers. 每条记录均由2个字节大小的小字节序组成。 I know the start position of each record and it´s size. 我知道每条记录的起始位置及其大小。

What´s the fastest way to read this to a Python array of integer? 将其读入Python整数数组的最快方法是什么?

我不认为您可以做得比打开文件并读取每个记录的大小更好,然后对要读取的每个整数使用struct.unpack('<i', buff)file.read(2)将得到你2个整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM