如何用Python从文件中读取字节
类似于 this 问题,我正在尝试阅读ID3v2 标记头,我无法弄清楚如何在 python 中获取单个字节。
我首先将所有十个字节读入一个字符串。 然后我想解析出各个信息。
我可以获取字符串中的两个版本号字符,但随后我不知道如何获取这两个字符并从中获取整数。
struct 包似乎是我想要的,但我无法让它工作。
到目前为止,这是我的代码(顺便说一句,我对 python 很陌生......所以对我来说放轻松):
def __init__(self, ten_byte_string):
self.whole_string = ten_byte_string
self.file_identifier = self.whole_string[:3]
self.major_version = struct.pack('x', self.whole_string[3:4]) #this
self.minor_version = struct.pack('x', self.whole_string[4:5]) # and this
self.flags = self.whole_string[5:6]
self.len = self.whole_string[6:10]
打印出除任何值之外的任何值显然是垃圾,因为它们的格式不正确。
Similar to this question, I am trying to read in an ID3v2 tag header and am having trouble figuring out how to get individual bytes in python.
I first read all ten bytes into a string. I then want to parse out the individual pieces of information.
I can grab the two version number chars in the string, but then I have no idea how to take those two chars and get an integer out of them.
The struct package seems to be what I want, but I can't get it to work.
Here is my code so-far (I am very new to python btw...so take it easy on me):
def __init__(self, ten_byte_string):
self.whole_string = ten_byte_string
self.file_identifier = self.whole_string[:3]
self.major_version = struct.pack('x', self.whole_string[3:4]) #this
self.minor_version = struct.pack('x', self.whole_string[4:5]) # and this
self.flags = self.whole_string[5:6]
self.len = self.whole_string[6:10]
Printing out any value except is obviously crap because they are not formatted correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您有一个包含 2 个字节的字符串,您希望将其解释为 16 位整数,则可以通过以下方式执行此
操作:请注意,> > 用于大尾数法(整数的最大部分在前)。 这是 id3 标签使用的格式。
对于其他大小的整数,您可以使用不同的格式代码。 例如。 “i”表示带符号的 32 位整数。 有关详细信息,请参阅帮助(结构)。
您还可以一次解压多个元素。 例如,对于 2 个无符号短整型,后跟一个带符号的 32 位值:
根据您的代码,您正在寻找(按顺序):
格式字符串为:
If you have a string, with 2 bytes that you wish to interpret as a 16 bit integer, you can do so by:
Note that the > is for big-endian (the largest part of the integer comes first). This is the format id3 tags use.
For other sizes of integer, you use different format codes. eg. "i" for a signed 32 bit integer. See help(struct) for details.
You can also unpack several elements at once. eg for 2 unsigned shorts, followed by a signed 32 bit value:
Going by your code, you are looking for (in order):
The format string for this would be:
为什么要自己写? (假设您还没有检查过这些其他选项。)有几个选项可用于在 Python 中从 MP3 读取 ID3 标签信息。 查看我的答案,网址为这个问题。
Why write your own? (Assuming you haven't checked out these other options.) There's a couple options out there for reading in ID3 tag info from MP3s in Python. Check out my answer over at this question.
FWIW,已经有一个模块。
FWIW, there's already a module for this.
我本来打算推荐 struct 包,但后来你说你已经尝试过了。 试试这个:
pack()
函数将 Python 数据类型转换为位,unpack()
函数将位转换为 Python 数据类型。I was going to recommend the
struct
package but then you said you had tried it. Try this:The
pack()
function convers Python data types to bits, and theunpack()
function converts bits to Python data types.