如何使用python解码编码的Excel文件 [英] How to decode an encoded excel file using python

查看:110
本文介绍了如何使用python解码编码的Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Java程序员将excel文件转换为二进制文件,然后将二进制文件内容发送给我.

My java programmer converted an excel file to binary and sending the binary content to me.

他使用 sun.misc.BASE64Encoder sun.misc.BASE64Decoder()进行编码.

我需要使用python将二进制数据转换为数据帧.

I need to convert that binary data to a data frame using python.

数据看起来像

UEsDBBQABgAIAAAAIQBi7p1oXgEAAJAEAAATAAgCW0NvbnRlbnRfVHl ........

我尝试了 bas64 解码器,但没有帮助.

I tried bas64 decoder but not helped.

我的代码:

import base64
with open('encoded_data.txt','rb') as d:
    data=d.read()
print(data)
`UEsDBBQABgAIAAAAIQBi7p1oXgEAAJAEAAATAAgCW0NvbnRlbnRfVHl........`
decrypted=base64.b64decode(data)
print(decrypt)
  'PK\x03\x04\x14\x00\x06\x00\x08\x00\x00\x00!\x00b\xee\x9dh^\x01\x00\x00\x90\x04\x00\x00\x13\x00\x08\x02[Content_Types].xml \xa2\x04\x02(\xa0\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00

请帮助我将此二进制数据转换为pandas数据框.

Please help me to convert this binary data to a pandas dataframe.

推荐答案

您快到了.由于解密的对象是一个字节字符串,为什么不使用 BytesIO ?

You're almost there. Since the decrypted object is a bytes string, why not use BytesIO?

import io
import pandas as pd

toread = io.BytesIO()
toread.write(decrypted)  # pass your `decrypted` string as the argument here
toread.seek(0)  # reset the pointer

df = pd.read_excel(toread)  # now read to dataframe

从评论中回答您的问题:如何将df转换为二进制编码的对象?

Answering your question from your comment: How to convert a df to a binary encoded object?

好吧,如果您想将其转换为b64编码的对象,而大熊猫将其转换为excel,则:

Well, if you want to convert it back to an b64 encoded object with pandas converting it as an excel, then:

towrite = io.BytesIO()
df.to_excel(towrite)  # write to BytesIO buffer
towrite.seek(0)  # reset pointer
encoded = base64.b64encode(towrite.read())  # encoded object

要将编码后的对象写入文件(只是为了关闭循环:P):

To write the encoded object to a file (just to close the loop :P):

with open("file.txt", "wb") as f:
    f.write(encoded)

这篇关于如何使用python解码编码的Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆