Python 将 BytesIO 转换为文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29324037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:24:02  来源:igfitidea点击:

Convert BytesIO into File

python

提问by K Engle

I have a BytesIO object containing the data of an excel document. The library I want to use doesn't support BytesIO and expects a File object instead. How can I take my BytesIO object and convert it into a File object?

我有一个包含 excel 文档数据的 BytesIO 对象。我想使用的库不支持 BytesIO,而是需要一个 File 对象。如何获取 BytesIO 对象并将其转换为 File 对象?

采纳答案by Reid Ballard

So, it would be a lot more helpful if you supplied the library you were using to work on excel files, but here's a buckshot of solutions, some of which may work, based on random assumptions I'm making due to complete lack of sample code:

因此,如果您提供用于处理 excel 文件的库会更有帮助,但这里有一些解决方案,其中一些可能有效,基于我由于完全缺乏样本而做出的随机假设代码:

  • Based on the first paragraph in the io module, it sounds like all the concrete classes- including BytesIO- are file-like objects. Without knowing what code you've tried so far, I don't know if you tried just passing the BytesIO to whatever module you're using.
  • On the off chance that doesn't work, you can simply convert BytesIO to a another io Writer/Reader/Wrapper by passing it to the constructor. Example:
  • 基于io 模块中的第一段,听起来所有具体的类——包括 BytesIO——都是类文件对象。在不知道到目前为止您尝试过哪些代码的情况下,我不知道您是否尝试将 BytesIO 传递给您正在使用的任何模块。
  • 如果不起作用,您可以简单地将 BytesIO 转换为另一个 io Writer/Reader/Wrapper,方法是将其传递给构造函数。例子:

.

.

import io

b = io.BytesIO(b"Hello World") ## Some random BytesIO Object
print(type(b))                 ## For sanity's sake
with open("test.xlsx") as f: ## Excel File
    print(type(f))           ## Open file is TextIOWrapper
    bw=io.TextIOWrapper(b)   ## Conversion to TextIOWrapper
    print(type(bw))          ## Just to confirm 
  • You may need to check which kind of Reader/Writer/Wrapper is expected by the module you're using to convert the BytesIO to the correct one
  • I believe I have heard that (for memory reasons, due to extremely large excel files) excel modules do not load the entire file. If this ends up meaning that what you need is a physical file on the disk, then you can easily write the Excel file temporarily and just delete it when you're done. Example:
  • 您可能需要检查您用于将 BytesIO 转换为正确的模块需要哪种类型的 Reader/Writer/Wrapper
  • 我相信我听说过(出于内存原因,由于 excel 文件非常大)excel 模块不会加载整个文件。如果这最终意味着您需要的是磁盘上的物理文件,那么您可以轻松地临时编写 Excel 文件,并在完成后将其删除。例子:

.

.

import io
import os

with open("test.xlsx",'rb') as f:
    g=io.BytesIO(f.read())   ## Getting an Excel File represented as a BytesIO Object
temporarylocation="testout.xlsx"
with open(temporarylocation,'wb') as out: ## Open temporary file as bytes
    out.write(g.read())                ## Read bytes into file

## Do stuff with module/file
os.remove(temporarylocation) ## Delete file when done

I'll hope that one of these points will solve your problem.

我希望这些要点之一可以解决您的问题。

回答by Martin Thoma

# Create an example
from io import BytesIO
bytesio_object = BytesIO(b"Hello World!")

# Write the stuff
with open("output.txt", "wb") as f:
    f.write(bytesio_object.getbuffer())

回答by johnson

pathlib.Path('file').write_bytes(io.BytesIO(b'data').getbuffer())