問題描述
我正在嘗試用 Python 讀取 BMP 文件.我知道前兩個字節表示 BMP 公司.接下來的 4 個字節是文件大小.當我執行時:
I'm trying to read a BMP file in Python. I know the first two bytes indicate the BMP firm. The next 4 bytes are the file size. When I execute:
fin = open("hi.bmp", "rb")
firm = fin.read(2)
file_size = int(fin.read(4))
我明白了:
ValueError: int() 以 10 為底的無效文字:'F#x13'
ValueError: invalid literal for int() with base 10: 'F#x13'
我想要做的是將這四個字節作為整數讀取,但似乎 Python 正在將它們作為字符讀取并返回一個字符串,該字符串無法轉換為整數.我怎樣才能正確地做到這一點?
What I want to do is reading those four bytes as an integer, but it seems Python is reading them as characters and returning a string, which cannot be converted to an integer. How can I do this correctly?
推薦答案
read
方法將字節序列作為字符串返回.要將字符串字節序列轉換為二進制數據,請使用內置 struct
模塊:http://docs.python.org/library/struct.html.
The read
method returns a sequence of bytes as a string. To convert from a string byte-sequence to binary data, use the built-in struct
module: http://docs.python.org/library/struct.html.
import struct
print(struct.unpack('i', fin.read(4)))
請注意,unpack
總是返回一個元組,所以 struct.unpack('i', fin.read(4))[0]
給出了你所需要的整數值正在追趕.
Note that unpack
always returns a tuple, so struct.unpack('i', fin.read(4))[0]
gives the integer value that you are after.
您可能應該使用格式字符串 '<i'
(< 是指示小端字節序和標準大小和對齊方式的修飾符 - 默認是使用平臺的字節順序、尺寸和對齊方式).根據 BMP 格式規范,字節應按 Intel/little-endian 字節順序寫入.
You should probably use the format string '<i'
(< is a modifier that indicates little-endian byte-order and standard size and alignment - the default is to use the platform's byte ordering, size and alignment). According to the BMP format spec, the bytes should be written in Intel/little-endian byte order.
這篇關于在 Python 中從二進制文件中讀取整數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!