pyarrow.PythonFile#
- class pyarrow.PythonFile#
基类:
NativeFile由 Python 文件对象支持的流。
此类允许将 Python 文件对象与任意 Arrow 函数配合使用,包括使用除 Python 以外的其他语言编写的函数。
其缺点是,在将 Arrow 流调用转换为 Python 方法调用时存在非零的重定向开销。此外,在某些情况下,Python 的全局解释器锁(GIL)可能会限制并行性。
示例
>>> import io >>> import pyarrow as pa >>> pa.PythonFile(io.BytesIO()) <pyarrow.PythonFile closed=False own_file=False is_seekable=False is_writable=True is_readable=False>
创建用于写入的流
>>> buf = io.BytesIO() >>> f = pa.PythonFile(buf, mode = 'w') >>> f.writable() True >>> f.write(b'PythonFile') 10 >>> buf.getvalue() b'PythonFile' >>> f.close() >>> f <pyarrow.PythonFile closed=True own_file=False is_seekable=False is_writable=True is_readable=False>
创建用于读取的流
>>> buf = io.BytesIO(b'PythonFile') >>> f = pa.PythonFile(buf, mode = 'r') >>> f.mode 'rb' >>> f.read() b'PythonFile' >>> f <pyarrow.PythonFile closed=False own_file=False is_seekable=True is_writable=False is_readable=True> >>> f.close() >>> f <pyarrow.PythonFile closed=True own_file=False is_seekable=True is_writable=False is_readable=True>
- __init__(*args, **kwargs)#
方法
__init__(*args, **kwargs)close(self)download(self, stream_or_path[, buffer_size])将此文件完整读取到本地路径或目标流中。
fileno(self)未实现
flush(self)刷新流(如果适用)。
get_stream(self, file_offset, nbytes)返回一个独立于文件状态的、用于读取文件片段的输入流。
isatty(self)metadata(self)返回文件元数据
read(self[, nbytes])读取并返回最多 n 个字节。
read1(self[, nbytes])读取并返回最多 n 个字节。
read_at(self, nbytes, offset)从文件的指定偏移量处读取指定数量的字节
read_buffer(self[, nbytes])从缓冲区读取。
readable(self)readall(self)readinto(self, b)读入提供的缓冲区
readline(self[, size])从文件中读取并返回一行字节。
readlines(self[, hint])读取文件的行。
seek(self, int64_t position, int whence=0)更改当前文件流位置
seekable(self)size(self)返回文件大小
tell(self)返回当前流位置
truncate(self[, pos])upload(self, stream[, buffer_size])从源流写入此文件。
writable(self)write(self, data)将数据写入文件。
writelines(self, lines)将行写入文件。
属性
- close(self)#
- closed#
- download(self, stream_or_path, buffer_size=None)#
将此文件完整读取到本地路径或目标流中。
此方法首先寻址(seek)到文件开头。
- fileno(self)#
未实现
- flush(self)#
刷新流(如果适用)。
如果流不可写,则会引发错误。
- get_stream(self, file_offset, nbytes)#
返回一个独立于文件状态的、用于读取文件片段的输入流。
允许将随机访问文件的部分内容作为输入流读取,且互不干扰。
- 参数:
- 返回:
- stream
NativeFile
- stream
- isatty(self)#
- metadata(self)#
返回文件元数据
- mode#
文件模式。目前 NativeFile 的实例可能支持:
rb: 二进制读取
wb: 二进制写入
rb+: 二进制读写
ab: 二进制追加
- read(self, nbytes=None)#
读取并返回最多 n 个字节。
如果 nbytes 为 None,则读取文件的剩余全部内容。
- read1(self, nbytes=None)#
读取并返回最多 n 个字节。
与 read() 不同,如果 nbytes 为 None,则只读取一个数据块,而不是整个文件。
- readable(self)#
- readall(self)#
- seek(self, int64_t position, int whence=0)#
更改当前文件流位置
备注
whence 的取值:* 0 – 流的开头(默认值);偏移量应为零或正数 * 1 – 当前流位置;偏移量可以是负数 * 2 – 流的末尾;偏移量通常为负数
- seekable(self)#
- size(self)#
返回文件大小
- tell(self)#
返回当前流位置
- upload(self, stream, buffer_size=None)#
从源流写入此文件。
- 参数:
- stream类文件对象
要管道传输到此文件的源流。
- buffer_size
int, 可选 用于数据传输的缓冲区大小。
- writable(self)#