在http://docs.python.org/library/json.html中:
simplejson.load(fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, use_decimal[, **kw]]]]]]]]])将fp(一个.read() - 支持包含JSON文档的类文件对象)反序列化为Python对象.
我所知道的read()和write()做的.
但是在阅读本说明书后"阅读() -支持类似文件的对象",我发现我不知道是什么对象类型支持read()和write().
我在其他文档中找不到.任何人都可以详细说明声明?
为什么我问这个问题是为了让"simplejson.load(urllib.open(...))"完成.
"urllib.open(...)"的返回值不是有效对象,因此我必须为simplejson定制它.但是,似乎该字符串不是read() - 支持.
Pho*_*nix 19
IO 文档中的 IO 类层次结构部分包含一个表格,列出了不同类型的类文件对象的内置方法和存根方法。
基本上,有一个抽象基类的层次结构:
IOBase,这几乎没有承诺
RawIOBase,它提供无缓冲的二进制 IOBufferedIOBase,它提供缓冲二进制 IOTextIOBase,它提供缓冲字符串IO要实现类似文件的对象,您可以对 的三个后代之一进行子类化IOBase,但不IOBase对其本身进行子类化。请参阅此答案以尝试确定给定的类文件对象是其中哪一个。
每个类都提供各种存根方法和混合:
| 班级 | 存根方法 | 混入 |
|---|---|---|
IOBase |
fileno, seek,truncate |
close,,,,,,,,,,,,,,,,,,,,,closed__enter____exit__flushisatty__iter____next__readablereadlinereadlinesseekabletellwritablewritelines |
RawIOBase |
readinto,write |
read,readall |
BufferedIOBase |
detach, read, read1,write |
readinto,readinto1 |
TextIOBase |
detach, read, readline,write |
encoding, errors,newlines |
这些方法的文档可以在上面链接的类的文档中找到。
che*_*ner 18
从词汇表:
文件对象的同义词
一个文件对象是
向底层资源公开面向文件的 API(使用 read() 或 write() 等方法)的对象。根据它的创建方式,文件对象可以调解对真实磁盘文件或另一种类型的存储或通信设备(例如标准输入/输出、内存缓冲区、套接字、管道等)的访问。 . 文件对象也称为类文件对象或流。
实际上有三类文件对象:原始二进制文件、缓冲二进制文件和文本文件。它们的接口在 io 模块中定义。创建文件对象的规范方法是使用 open() 函数。
Adr*_*n W 13
在 Python 中,文件对象是一个公开 API 的对象,该 API 具有执行通常对文件执行的操作的方法,例如read()或write()。
在问题的示例中:simplejson.load(fp, ...),传递的对象fp只需要有一个read()方法,可以像read()文件上的 a一样调用(即接受可选参数size并返回 astr或bytes对象)。
不过,只要它有一个read()方法,它就不需要是一个真正的文件。
类文件对象只是file-object的同义词。请参阅Python 词汇表。
类文件对象主要是StringIO对象,连接套接字和实际文件对象.如果一切顺利,urllib.urlopen()还会返回一个类似文件的objekt,支持必要的方法.
这是 Python 标准库中所有类文件对象的 API(截至 3.10.5)。
# All file-like objects inherit the IOBase interface:
# Documented at https://docs.python.org/3/library/io.html#io.IOBase .
close() -> None
closed() -> bool # Implemented as @property `closed`
fileno() -> int
flush() -> None
isatty() -> bool
readable() -> bool
readline(size: int = -1) -> Union[str, bytes]
readlines(hint: Union[int, None] = None) -> list
seek(pos: int, whence: int = io.SEEK_SET) -> int # SEEK_SET is 0
seekable() -> bool
tell() -> int
truncate(pos: int = None) -> int # The parameter is named "size" in class FileIO
writable() -> bool
writelines(lines: list) -> None
__del__() -> None
# Documented at https://docs.python.org/3/library/io.html#class-hierarchy .
__enter__()
__exit__(*args) -> None:
__iter__()
__next__() -> Union[str, bytes]
# Documented in paragraph at https://docs.python.org/3/library/io.html#io.IOBase .
# Note that while the documentation claims that the method signatures
# of `read` and `write` vary, all file-like objects included in the Python
# Standard Library have the following exact method signatures for `read` and `write`:
read(size: int = -1) -> Union[str, bytes]
write(b: Union[str, bytes]) -> int # The parameter is named "s" in TextIOBase
Run Code Online (Sandbox Code Playgroud)
特定的类文件对象可能实现的不止于此,但这是所有类文件对象所共有的方法的子集。
| 归档时间: |
|
| 查看次数: |
9982 次 |
| 最近记录: |