簡體   English   中英

Python 中的類文件對象到底是什么?

[英]What is exactly a file-like object in Python?

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()-supporting file-like object”之后,我發現我不知道什么對象類型支持read()write()

我在其余的文檔中找不到。 任何人都可以詳細說明該聲明嗎?

為什么我問這個問題是為了完成“simplejson.load(urllib.open(...))”。
“urllib.open(...)”的返回值不是一個有效的對象,所以我必須為simplejson定制它。 但是,似乎該字符串不支持 read()。

詞匯表

類文件對象

文件對象的同義詞

和一個文件對象是

文件對象

向底層資源公開面向文件的 API(使用 read() 或 write() 等方法)的對象。 根據創建的方式,文件對象可以調解對真實磁盤文件或其他類型的存儲或通信設備(例如標准輸入/輸出、內存緩沖區、套接字、管道等)的訪問。 . 文件對象也稱為類文件對象或流。

實際上存在三類文件對象:原始二進制文件、緩沖二進制文件和文本文件。 它們的接口在 io 模塊中定義。 創建文件對象的規范方法是使用 open() 函數。

在 Python 中,文件對象是公開 API 的對象,該 API 具有用於執行通常對文件執行的操作的方法,例如read()write()

在問題的示例中: simplejson.load(fp, ...) ,作為fp傳遞的對象只需要有一個read()方法,可調用方式與文件上的read()相同(即接受可選參數size並返回strbytes對象)。

不過,它不需要是一個真實的文件,只要它有一個read()方法。

類似文件的對象只是file-object的同義詞。 請參閱Python 詞匯表

類文件對象主要是StringIO對象、連接的套接字以及實際的文件對象。

如果一切順利, urllib.urlopen()會返回一個支持必要方法的類文件對象。

IO 文檔中的IO 類層次結構部分包含一個表格,其中列出了不同類型的類文件對象的內置方法和存根方法。

基本上,抽象基類有一個層次結構:

要實現類似文件的對象,您可以繼承IOBase的三個后代之一,但不是IOBase本身。 請參閱此答案以嘗試確定給定的類文件對象中的哪一個。

這些類中的每一個都提供了各種存根方法和 mixin:

班級 存根方法 混合
IOBase fileno號, seektruncate close , closed , __enter__ , __exit__ , flush , isatty , __iter__ , __next__ , readable , readline , readlines , seekable , tell , writable , writelines
RawIOBase readintowrite read readall
BufferedIOBase detachreadread1write readintoreadinto1
TextIOBase detachreadreadlinewrite encoding , errors , newlines

這些方法的文檔可以在上面鏈接的類文檔中找到。

這是 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

特定的類文件對象可能實現的不止於此,但這是所有類文件對象共有的方法的子集。

simplejson 的調用加載轉儲消耗和生成字符串,而不是像文件這樣的對象。

此鏈接在 StringIO 和 simplejson 的上下文中為類文件和字符串對象提供了一個示例。

http://svn.red-bean.com/bob/simplejson/tags/simplejson-1.3/docs/index.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM