.read() return 是一个流吗?
Does .read() return a stream?
python 中 .read()
操作的类型 return 的技术术语是什么?
例如:
x = open('myfile', 'r').read()
x = stdin.read()
x
指的是什么?它是字节流吗?我想创建一个需要 x 作为输入的函数,但不知道如何在我的文档字符串中解释它?浏览器
def parser(stream, ...):
""" First argument is ??? type, returned from .read() operations."""
我试过谷歌搜索,但大多数结果都太专业了。
谢谢
来自 https://docs.python.org/2/library/stdtypes.html#bltin-file-objects
file.read([size])
Read at most size bytes from the file (less if the read hits EOF before obtaining size bytes). If the size argument is negative or omitted, read all data until EOF is reached. The bytes are returned as a string object. An empty string is returned when EOF is encountered immediately. (For certain files, like ttys, it makes sense to continue reading after an EOF is hit.) Note that this method may call the underlying C function fread() more than once in an effort to acquire as close to size bytes as possible. Also note that when in non-blocking mode, less data than was requested may be returned, even if no size parameter was given.
什么地方太啰嗦了
The bytes are returned as a string object.
这是一个字符串,包含了文件的所有内容。
编辑:亚当,Python 通常不是严格输入的。任何对类似字符串的对象起作用的东西通常都会接受任何具有 methods/properties 的字符串。
它是一个 unicode 字符串或一个 bytestring,这取决于你打开文件的模式和你的 Python 版本。
- Python 2:总是returns一个字节串(类型
str
)
- Python 3: Returns Unicode 字符串(类型
str
)除非 "b"
处于模式中,在这种情况下它 returns 类型 bytes
python 中 .read()
操作的类型 return 的技术术语是什么?
例如:
x = open('myfile', 'r').read()
x = stdin.read()
x
指的是什么?它是字节流吗?我想创建一个需要 x 作为输入的函数,但不知道如何在我的文档字符串中解释它?浏览器
def parser(stream, ...):
""" First argument is ??? type, returned from .read() operations."""
我试过谷歌搜索,但大多数结果都太专业了。
谢谢
来自 https://docs.python.org/2/library/stdtypes.html#bltin-file-objects
file.read([size])
Read at most size bytes from the file (less if the read hits EOF before obtaining size bytes). If the size argument is negative or omitted, read all data until EOF is reached. The bytes are returned as a string object. An empty string is returned when EOF is encountered immediately. (For certain files, like ttys, it makes sense to continue reading after an EOF is hit.) Note that this method may call the underlying C function fread() more than once in an effort to acquire as close to size bytes as possible. Also note that when in non-blocking mode, less data than was requested may be returned, even if no size parameter was given.
什么地方太啰嗦了
The bytes are returned as a string object.
这是一个字符串,包含了文件的所有内容。
编辑:亚当,Python 通常不是严格输入的。任何对类似字符串的对象起作用的东西通常都会接受任何具有 methods/properties 的字符串。
它是一个 unicode 字符串或一个 bytestring,这取决于你打开文件的模式和你的 Python 版本。
- Python 2:总是returns一个字节串(类型
str
) - Python 3: Returns Unicode 字符串(类型
str
)除非"b"
处于模式中,在这种情况下它 returns 类型bytes