“Python/docs/3.9/library/chunk”的版本间差异

来自菜鸟教程
Python/docs/3.9/library/chunk
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:chunk — 读取 IFF 分块数据 — Python 文档}}
 
<div id="module-chunk" class="section">
 
<div id="module-chunk" class="section">
  
 
<span id="chunk-read-iff-chunked-data"></span>
 
<span id="chunk-read-iff-chunked-data"></span>
= [[#module-chunk|<code>chunk</code>]] --- Read IFF chunked data =
+
= chunk — 读取 IFF 分块数据 =
  
'''Source code:''' [https://github.com/python/cpython/tree/3.9/Lib/chunk.py Lib/chunk.py]
+
'''源代码:''' [[#id1|<span id="id2" class="problematic">:source:`Lib/chunk.py`</span>]]
  
This module provides an interface for reading files that use EA IFF 85 chunks.
 
[[#id2|1]] This format is used in at least the Audio Interchange File Format
 
(AIFF/AIFF-C) and the Real Media File Format (RMFF). The WAVE audio file format
 
is closely related and can also be read using this module.
 
  
A chunk has the following structure:
+
-----
 +
 
 +
该模块提供了一个用于读取使用 EA IFF 85 块的文件的接口。 [[#id4|1]] 该格式至少用于音频交换文件格式(AIFF/AIFF-C)和真实媒体文件格式(RMFF)。 WAVE 音频文件格式密切相关,也可以使用此模块读取。
 +
 
 +
一个块具有以下结构:
  
 
{|
 
{|
!width="19%"| Offset
+
!width="19%"| 抵消
!width="17%"| Length
+
!width="17%"| 长度
!width="65%"| Contents
+
!width="65%"| 内容
 
|-
 
|-
 
| 0
 
| 0
 
| 4
 
| 4
| Chunk ID
+
| 区块 ID
 
|-
 
|-
 
| 4
 
| 4
 
| 4
 
| 4
| Size of chunk in big-endian
+
| 大端字节序的块大小,不包括头
byte order, not including the
 
header
 
 
|-
 
|-
 
| 8
 
| 8
 
| ''n''
 
| ''n''
| Data bytes, where ''n'' is the
+
| 数据字节,其中 ''n'' 是前面字段中给出的大小
size given in the preceding
 
field
 
 
|-
 
|-
 
| 8 + ''n''
 
| 8 + ''n''
| 0 or 1
+
| 0 1
| Pad byte needed if ''n'' is odd
+
| 如果 ''n'' 是奇数并且使用块对齐,则需要填充字节
and chunk alignment is used
 
 
|}
 
|}
  
The ID is a 4-byte string which identifies the type of chunk.
+
ID 是一个 4 字节的字符串,用于标识块的类型。
  
The size field (a 32-bit value, encoded using big-endian byte order) gives the
+
大小字段(一个 32 位值,使用大端字节序编码)给出了块数据的大小,不包括 8 字节的标头。
size of the chunk data, not including the 8-byte header.
 
  
Usually an IFF-type file consists of one or more chunks. The proposed usage of
+
通常一个 IFF 类型的文件由一个或多个块组成。 这里定义的 [[#chunk.Chunk|Chunk]] 类的建议用法是在每个块的开头实例化一个实例,并从该实例中读取直到它到达末尾,之后可以实例化一个新的实例。 在文件末尾,创建新实例将失败,并出现 [[../exceptions#EOFError|EOFError]] 异常。
the [[#chunk.Chunk|<code>Chunk</code>]] class defined here is to instantiate an instance at the start
 
of each chunk and read from the instance until it reaches the end, after which a
 
new instance can be instantiated. At the end of the file, creating a new
 
instance will fail with an [[../exceptions#EOFError|<code>EOFError</code>]] exception.
 
  
 
<dl>
 
<dl>
<dt>''class'' <code>chunk.</code><code>Chunk</code><span class="sig-paren">(</span>''<span class="n">file</span>'', ''<span class="n">align</span><span class="o">=</span><span class="default_value">True</span>'', ''<span class="n">bigendian</span><span class="o">=</span><span class="default_value">True</span>'', ''<span class="n">inclheader</span><span class="o">=</span><span class="default_value">False</span>''<span class="sig-paren">)</span></dt>
+
<dt>''<span class="pre">class</span>'' <span class="sig-prename descclassname"><span class="pre">chunk.</span></span><span class="sig-name descname"><span class="pre">Chunk</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">file</span></span>'', ''<span class="n"><span class="pre">align</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span>'', ''<span class="n"><span class="pre">bigendian</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span>'', ''<span class="n"><span class="pre">inclheader</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Class which represents a chunk. The ''file'' argument is expected to be a
+
<dd><p>代表一个块的类。 ''file'' 参数应该是一个类似文件的对象。 此类的实例是特别允许的。 唯一需要的方法是<code>read()</code>。 如果方法 [[../io#io.IOBase|seek()]] [[../io#io.IOBase|tell()]] 存在并且不引发异常,它们也会被使用。 如果这些方法存在并引发异常,则预计它们不会更改对象。 如果可选参数 ''align'' 为真,则假定块在 2 字节边界上对齐。 如果 ''align'' 为假,则假定没有对齐。 默认值是true。 如果可选参数 ''bigendian'' 为 false,则假定块大小为 little-endian 顺序。 这是 WAVE 音频文件所需要的。 默认值是true。 如果可选参数 ''inclheader'' 为真,则块头中给出的大小包括头的大小。 默认值为假。</p>
file-like object. An instance of this class is specifically allowed. The
+
<p>[[#chunk.Chunk|Chunk]] 对象支持以下方法:</p>
only method that is needed is <code>read()</code>. If the methods
 
[[../io#io.IOBase|<code>seek()</code>]] and [[../io#io.IOBase|<code>tell()</code>]] are present and don't
 
raise an exception, they are also used.
 
If these methods are present and raise an exception, they are expected to not
 
have altered the object. If the optional argument ''align'' is true, chunks
 
are assumed to be aligned on 2-byte boundaries. If ''align'' is false, no
 
alignment is assumed. The default value is true. If the optional argument
 
''bigendian'' is false, the chunk size is assumed to be in little-endian order.
 
This is needed for WAVE audio files. The default value is true. If the
 
optional argument ''inclheader'' is true, the size given in the chunk header
 
includes the size of the header. The default value is false.</p>
 
<p>A [[#chunk.Chunk|<code>Chunk</code>]] object supports the following methods:</p>
 
 
<dl>
 
<dl>
<dt><code>getname</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-name descname"><span class="pre">getname</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
<dd><p>Returns the name (ID) of the chunk. This is the first 4 bytes of the
+
<dd><p>返回块的名称 (ID)。 这是块的前 4 个字节。</p></dd></dl>
chunk.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt><code>getsize</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-name descname"><span class="pre">getsize</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
<dd><p>Returns the size of the chunk.</p></dd></dl>
+
<dd><p>返回块的大小。</p></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>close</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
<dd><p>Close and skip to the end of the chunk. This does not close the
+
<dd><p>关闭并跳到块的末尾。 这不会关闭基础文件。</p></dd></dl>
underlying file.</p></dd></dl>
 
  
<p>The remaining methods will raise [[../exceptions#OSError|<code>OSError</code>]] if called after the
+
<p>如果在调用 [[#chunk.Chunk.close|close()]] 方法之后调用,其余方法将引发 [[../exceptions#OSError|OSError]]。 在 Python 3.3 之前,他们曾经提出 [[../exceptions#IOError|IOError]],现在是 [[../exceptions#OSError|OSError]] 的别名。</p>
[[#chunk.Chunk.close|<code>close()</code>]] method has been called. Before Python 3.3, they used to
 
raise [[../exceptions#IOError|<code>IOError</code>]], now an alias of [[../exceptions#OSError|<code>OSError</code>]].</p>
 
 
<dl>
 
<dl>
<dt><code>isatty</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-name descname"><span class="pre">isatty</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
<dd><p>Returns <code>False</code>.</p></dd></dl>
+
<dd><p>返回 <code>False</code></p></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>seek</code><span class="sig-paren">(</span>''<span class="n">pos</span>'', ''<span class="n">whence</span><span class="o">=</span><span class="default_value">0</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-name descname"><span class="pre">seek</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">pos</span></span>'', ''<span class="n"><span class="pre">whence</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Set the chunk's current position. The ''whence'' argument is optional and
+
<dd><p>设置块的当前位置。 ''whence'' 参数可选,默认为 <code>0</code>(绝对文件定位); 其他值是 <code>1</code>(相对于当前位置搜索)和 <code>2</code>(相对于文件末尾搜索)。 没有返回值。 如果底层文件不允许搜索,则只允许向前搜索。</p></dd></dl>
defaults to <code>0</code> (absolute file positioning); other values are <code>1</code>
 
(seek relative to the current position) and <code>2</code> (seek relative to the
 
file's end). There is no return value. If the underlying file does not
 
allow seek, only forward seeks are allowed.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt><code>tell</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-name descname"><span class="pre">tell</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
<dd><p>Return the current position into the chunk.</p></dd></dl>
+
<dd><p>将当前位置返回到块中。</p></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>read</code><span class="sig-paren">(</span>''<span class="n">size</span><span class="o">=</span><span class="default_value">- 1</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-name descname"><span class="pre">read</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-</span> <span class="pre">1</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Read at most ''size'' bytes from the chunk (less if the read hits the end of
+
<dd><p>从块中最多读取 ''size'' 个字节(如果在获取 ''size'' 个字节之前读取到达块的末尾,则更少)。 如果 ''size'' 参数为负数或省略,则读取所有数据,直到块结束。 当立即遇到块的结尾时,将返回一个空字节对象。</p></dd></dl>
the chunk before obtaining ''size'' bytes). If the ''size'' argument is
 
negative or omitted, read all data until the end of the chunk. An empty
 
bytes object is returned when the end of the chunk is encountered
 
immediately.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt><code>skip</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-name descname"><span class="pre">skip</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
<dd><p>Skip to the end of the chunk. All further calls to [[#chunk.Chunk.read|<code>read()</code>]] for the
+
<dd><p>跳到块的末尾。 对该块的所有进一步调用 [[#chunk.Chunk.read|read()]] 将返回 <code>b''</code>。 如果您对块的内容不感兴趣,则应调用此方法以使文件指向下一个块的开头。</p></dd></dl>
chunk will return <code>b''</code>. If you are not interested in the contents of
 
the chunk, this method should be called so that the file points to the
 
start of the next chunk.</p></dd></dl>
 
 
</dd></dl>
 
</dd></dl>
  
Footnotes
+
脚注
 +
 
 +
; <span class="brackets">[[#id3|1]]</span>
 +
: “EA IFF 85”交换格式文件标准,Jerry Morrison,Electronic Arts,1985 年 1 月。
 +
 
 +
 
 +
</div>
 +
<div class="clearer">
  
; <span class="brackets">[[#id1|1]]</span>
 
: &quot;EA IFF 85&quot; Standard for Interchange Format Files, Jerry Morrison, Electronic Arts, January 1985.
 
  
  
 
</div>
 
</div>
  
[[Category:Python 3.9 中文文档]]
+
[[Category:Python 3.9 文档]]

2021年10月31日 (日) 04:51的最新版本

chunk — 读取 IFF 分块数据

源代码: :source:`Lib/chunk.py`



该模块提供了一个用于读取使用 EA IFF 85 块的文件的接口。 1 该格式至少用于音频交换文件格式(AIFF/AIFF-C)和真实媒体文件格式(RMFF)。 WAVE 音频文件格式密切相关,也可以使用此模块读取。

一个块具有以下结构:

抵消 长度 内容
0 4 区块 ID
4 4 大端字节序的块大小,不包括头
8 n 数据字节,其中 n 是前面字段中给出的大小
8 + n 0 或 1 如果 n 是奇数并且使用块对齐,则需要填充字节

ID 是一个 4 字节的字符串,用于标识块的类型。

大小字段(一个 32 位值,使用大端字节序编码)给出了块数据的大小,不包括 8 字节的标头。

通常一个 IFF 类型的文件由一个或多个块组成。 这里定义的 Chunk 类的建议用法是在每个块的开头实例化一个实例,并从该实例中读取直到它到达末尾,之后可以实例化一个新的实例。 在文件末尾,创建新实例将失败,并出现 EOFError 异常。

class chunk.Chunk(file, align=True, bigendian=True, inclheader=False)

代表一个块的类。 file 参数应该是一个类似文件的对象。 此类的实例是特别允许的。 唯一需要的方法是read()。 如果方法 seek()tell() 存在并且不引发异常,它们也会被使用。 如果这些方法存在并引发异常,则预计它们不会更改对象。 如果可选参数 align 为真,则假定块在 2 字节边界上对齐。 如果 align 为假,则假定没有对齐。 默认值是true。 如果可选参数 bigendian 为 false,则假定块大小为 little-endian 顺序。 这是 WAVE 音频文件所需要的。 默认值是true。 如果可选参数 inclheader 为真,则块头中给出的大小包括头的大小。 默认值为假。

Chunk 对象支持以下方法:

getname()

返回块的名称 (ID)。 这是块的前 4 个字节。

getsize()

返回块的大小。

close()

关闭并跳到块的末尾。 这不会关闭基础文件。

如果在调用 close() 方法之后调用,其余方法将引发 OSError。 在 Python 3.3 之前,他们曾经提出 IOError,现在是 OSError 的别名。

isatty()

返回 False

seek(pos, whence=0)

设置块的当前位置。 whence 参数可选,默认为 0(绝对文件定位); 其他值是 1(相对于当前位置搜索)和 2(相对于文件末尾搜索)。 没有返回值。 如果底层文件不允许搜索,则只允许向前搜索。

tell()

将当前位置返回到块中。

read(size=- 1)

从块中最多读取 size 个字节(如果在获取 size 个字节之前读取到达块的末尾,则更少)。 如果 size 参数为负数或省略,则读取所有数据,直到块结束。 当立即遇到块的结尾时,将返回一个空字节对象。

skip()

跳到块的末尾。 对该块的所有进一步调用 read() 将返回 b。 如果您对块的内容不感兴趣,则应调用此方法以使文件指向下一个块的开头。

脚注

1
“EA IFF 85”交换格式文件标准,Jerry Morrison,Electronic Arts,1985 年 1 月。