Pandas python,工作簿编码类型是什么?
Pandas python, what is the workbook encoding type?
我是 python 的新手,也是 Python 中的 pandas 库的新手。文档没有很好地描述,他们也没有很好地解释。我想将数据帧保存为 excel 格式并 在内存中 我发现了以下解释:
[Pandas excel to the memory]
我需要关于 workbook
的解释。这个变量的值是经过编码的,我怎么才能看到这个变量的真实值呢?如何解码?它的 return 值应该是多少?
已编辑:
如何将其传递到Mandrill的附件内容中api。
https://mandrillapp.com/api/docs/messages.python.html
这是我 excel extension 的附件部分:
'attachments': [
{
'content': content,
'name': 'fraud_report.xlsx',
'type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
}
我无法打开 excel 文件,而且我一直收到来自 Microsoft excel 的错误,其中显示 the file format is not valid!...
任何帮助都会有所帮助。谢谢
为了便于解释,我再次将您 link 中的示例粘贴到此处:
# Safe import for either Python 2.x or 3.x
try:
from io import BytesIO
except ImportError:
from cStringIO import StringIO as BytesIO
bio = BytesIO()
# By setting the 'engine' in the ExcelWriter constructor.
writer = ExcelWriter(bio, engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
# Save the workbook
writer.save()
# Seek to the beginning and read to copy the workbook to a variable in memory
bio.seek(0)
workbook = bio.read()
方法 writer.save()
将数据保存在 BytesIO (bio
) 而不是 Excel 文件中。这意味着,变量 bio
存储 excel 文件的字节码。
方法bio.seek(0)
将bio
的当前位置(读、写...)设置为0。这样就可以从下一个方法bio.read()
.
开始读取bio
的数据了
变量workbook
存储excel 文件(或excel 工作簿)的字节字符串。如果你以字节模式读取一个excel文件,你会得到相同的数据。或者你可以把它写在一个 excel 文件中:
with open("my_excel_file.xlsx", "wb") as f:
f.write(workbook)
要从 bio
读取数据并存储在 DataFrame 中,您不需要 bio.read()
:
bio.seek(0)
df = pd.read_excel(bio, "Sheet1", engine="xlrd")
关于mandrill的使用问题:
在mandrill的例子中你看到:
{'attachments': [{'content': 'ZXhhbXBsZSBmaWxl',
'name': 'myfile.txt',
'type': 'text/plain'}],...
文档也写了:
content: the content of the attachment as a base64-encoded string
您应该在 base64 中对 workbook
进行编码并将其用于发送
import base64
content = base64.b64encode(workbook)
P/S:workbook
和 content
的类型为 bytes
。可能您需要在发送前将 content
转换为 str
。
{'attachments': [{'content': content.decode('utf-8'),
'name': 'myfile.xlsx',
'type': 'text/plain'}],...
添加:如果文件是excel那么你应该把type
改成application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
我是 python 的新手,也是 Python 中的 pandas 库的新手。文档没有很好地描述,他们也没有很好地解释。我想将数据帧保存为 excel 格式并 在内存中 我发现了以下解释: [Pandas excel to the memory]
我需要关于 workbook
的解释。这个变量的值是经过编码的,我怎么才能看到这个变量的真实值呢?如何解码?它的 return 值应该是多少?
已编辑:
如何将其传递到Mandrill的附件内容中api。
https://mandrillapp.com/api/docs/messages.python.html
这是我 excel extension 的附件部分:
'attachments': [
{
'content': content,
'name': 'fraud_report.xlsx',
'type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
}
我无法打开 excel 文件,而且我一直收到来自 Microsoft excel 的错误,其中显示 the file format is not valid!...
任何帮助都会有所帮助。谢谢
为了便于解释,我再次将您 link 中的示例粘贴到此处:
# Safe import for either Python 2.x or 3.x
try:
from io import BytesIO
except ImportError:
from cStringIO import StringIO as BytesIO
bio = BytesIO()
# By setting the 'engine' in the ExcelWriter constructor.
writer = ExcelWriter(bio, engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
# Save the workbook
writer.save()
# Seek to the beginning and read to copy the workbook to a variable in memory
bio.seek(0)
workbook = bio.read()
方法 writer.save()
将数据保存在 BytesIO (bio
) 而不是 Excel 文件中。这意味着,变量 bio
存储 excel 文件的字节码。
方法bio.seek(0)
将bio
的当前位置(读、写...)设置为0。这样就可以从下一个方法bio.read()
.
bio
的数据了
变量workbook
存储excel 文件(或excel 工作簿)的字节字符串。如果你以字节模式读取一个excel文件,你会得到相同的数据。或者你可以把它写在一个 excel 文件中:
with open("my_excel_file.xlsx", "wb") as f:
f.write(workbook)
要从 bio
读取数据并存储在 DataFrame 中,您不需要 bio.read()
:
bio.seek(0)
df = pd.read_excel(bio, "Sheet1", engine="xlrd")
关于mandrill的使用问题:
在mandrill的例子中你看到:
{'attachments': [{'content': 'ZXhhbXBsZSBmaWxl',
'name': 'myfile.txt',
'type': 'text/plain'}],...
文档也写了:
content: the content of the attachment as a base64-encoded string
您应该在 base64 中对 workbook
进行编码并将其用于发送
import base64
content = base64.b64encode(workbook)
P/S:workbook
和 content
的类型为 bytes
。可能您需要在发送前将 content
转换为 str
。
{'attachments': [{'content': content.decode('utf-8'),
'name': 'myfile.xlsx',
'type': 'text/plain'}],...
添加:如果文件是excel那么你应该把type
改成application/vnd.openxmlformats-officedocument.spreadsheetml.sheet