有没有办法将文件复制到内存中以进行编辑?

huangapple go评论65阅读模式
英文:

Is there a way to copy a file to memory to edit it?

问题

我正在通过电子邮件发送作为Excel文件的pandas数据帧。

直到现在,我使用的是pandas 1.5.3,它允许我从模板Excel文件中复制工作簿到我的ExcelWriter的工作簿。我会创建一个缓冲区,然后将其用作我的ExcelWriterpath参数,以便我可以使用写入器插入数据帧的数据,然后通过返回buffer.getbuffer()来返回我的电子邮件附件。

以下是我旧代码的简化示例(我不知道是否使用BytesIO对象是一种好做法):

def insert_template(dataframe):
    path_template = "templates/foo.xlsx"

    buffer = BytesIO()

    book = load_workbook(path_template)

    with ExcelWriter(buffer, engine="openpyxl") as writer:
        writer.book = book
        sheet_name = book.sheetnames[0]
        book.close()

        dataframe.to_excel(writer, sheet_name=sheet_name, index=False)

        buffer.seek(0)
    return buffer.getbuffer()

现在我使用pandas 2.x,我不能再分配给writer.book,这迫使我首先复制我的模板文件(按照这个答案的方法),然后编辑此文件并通过电子邮件发送它。

以下是我的新代码的简化示例:

def insert_template(dataframe):
    path_template = "templates/foo.xlsx"
    file_path = shutil.copy(path_template, './')

    with ExcelWriter(file_path, engine="openpyxl", mode="a", if_sheet_exists="overlay") as writer:
        sheet_name = writer.book.sheetnames[0]

        dataframe.to_excel(writer, sheet_name=sheet_name, index=False)

    return file_path

问题是,现在我必须使用shutil.copy(它需要路径样式或字符串对象作为参数),我必须在我的目录中创建和删除文件,这让我很困扰。有没有办法在内存中使用文件?如果有,最佳做法是什么?

英文:

I am sending pandas dataframes by e-mail as excel files.

Until now, I used pandas 1.5.3, which let me copy the workbook from a template excel file to my ExcelWriter's workbook. I would create a buffer, that I would use as my ExcelWriter's path parameter, so that I could insert the dataframe's data with the writer and then return what would be my e-mail attachment by returning buffer.getbuffer().

Here is a simplified sample of my old code (I have no idea whether it is good practice to use a BytesIO object for this kind of things) :

def insert_template(dataframe) :
	path_template= f"templates\foo.xlsx"
	
	buffer = BytesIO()
	
	book = load_workbook(path_fichier_modele)
	
	with ExcelWriter(buffer, engine="openpyxl") as writer:
		writer.book = book
		sheet_name = book.sheetnames[0]
		book.close()
		
		dataframe.to_excel(writer, sheet_name=sheet_name, index=False)
		
		buffer.seek(0)
	return buffer.getbuffer()

Now that I use pandas 2.x, I can no longer assign to writer.book, which forces me to copy my template file first (following this answer), then edit this file and send it by e-mail.

Here is a simplified sample of my new code :

def insert_template(dataframe) :
	path_template = f"templates\foo.xlsx"
	file_path = shutil.copy(path_template, '.\\')

	with ExcelWriter(file_path, engine="openpyxl", mode="a", if_sheet_exists="overlay") as writer:
		sheet_name = writer.book.sheetnames[0]

		dataframe.to_excel(writer, sheet_name=sheet_name, index=False)

	return file_path

The Issue is that now that I have to use shutil.copy (which requires path-like or string objects as parameters), I have to create and delete a file in my directory, which bugs me a lot. Is there no way to work with a file in memory ? If so, what is the best practice ?

答案1

得分: 1

你可以使用不同的shutil函数shutil.copyfileobj()来完成这个任务。

示例:

import shutil
from io import BytesIO

buf = BytesIO()
with open('test.txt', 'rb') as f_in:
    shutil.copyfileobj(f_in, buf)

之后,buf将包含文件test.txt的内容。

你可能还需要buf.seek(0)来重置读/写指针的位置。

英文:

You can use a different shutil function, shutil.copyfileobj() to do this.

Example:

import shutil
from io import BytesIO


buf = BytesIO()
with open('test.txt', 'rb') as f_in:
    shutil.copyfileobj(f_in, buf)

After that, buf will contain the contents of the file test.txt.

You may also need buf.seek(0) to reset the position of the read/write pointer.

huangapple
  • 本文由 发表于 2023年6月19日 22:38:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76507666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定