擅长:python、mysql、java
<p>格式说明符中的数字表示重复计数,但它必须在字母之前,如<code>'<8d'</code>。不过,您说过只想读取结构的一个元素。我想你只是想<code>'<d'</code>。我想您正在尝试将要读取的字节数指定为8,但不需要这样做。<code>d</code>假设是这样。</p>
<p>我还注意到你在使用<code>readline</code>。读取二进制数据似乎是错误的。它将一直读取,直到下一个回车/换行,这将在二进制数据中随机出现。您要做的是使用<code>read(size)</code>,如下所示:</p>
<pre><code>part_struct = part.read(8)
r = struct.unpack('<d', part_struct)
</code></pre>
<p>实际上,您应该小心,因为<code>read</code>返回的数据可能少于您的请求。你需要重复一遍。</p>
<pre><code>part_struct = b''
while len(part_struct) < 8:
data = part.read(8 - len(part_struct))
if not data: raise IOException("unexpected end of file")
part_struct += data
r = struct.unpack('<d', part_struct)
</code></pre>