我有一个小应用程序,它使用以下方式读取本地文件:
open(diefile_path, 'r') as csv_file
open(diefile_path, 'r') as file
and also uses linecache module
我需要将用途扩展到从远程服务器发送的文件。
服务器接收到的内容类型是字节。
我找不到很多有关处理 IOBytes 类型的信息,我想知道是否有一种方法可以将字节块转换为类似文件的对象。
我的目标是使用上面指定的 API ( open, linecache)
我能够使用 将字节转换为字符串data.decode("utf-8"),
但我不能使用上面的方法 (open和linecache)
一个小例子来说明
data = 'b'First line\nSecond line\nThird line\n'
with open(data) as file:
line = file.readline()
print(line)
Run Code Online (Sandbox Code Playgroud)
输出:
First line
Second line
Third line
Run Code Online (Sandbox Code Playgroud)
可以吗?