如何使用Python正确编码/解码Excel文件?

时间:2019-05-22 15:58:40

标签: python azure binary azure-storage-blobs

我知道有一些类似的问题,尽管它们都没有帮助我解决问题。最终目标是拥有一个flask应用程序,该应用程序需要一个excel文件,并将其存储在蔚蓝斑点存储中,然后由我的python函数应用程序用来进行一些进一步的转换。我要解决的是如何在必须使用时对该文件进行编码/解码 block_blob_service.create_blob_from_bytes()功能。

我唯一想到的就是使用Pandas库读取此excel,然后读取tobytes()函数。这样,我就可以将excel作为CSV文件上传到Blob。但是,我无法真正将其转换为以前的形式。

打开后的样子:

9]�0��j�9p/�j���`��/ wj1 = p /�j��p�^�.wj2= p /�[...]

尝试用utf-8解码给了我一些永无止境的错误,说'utf-8'编解码器无法解码字节[...]我尝试了许多不同的编码,但是它总是以以下消息结尾一些字节。 Excel包含数字,字符串和日期。

因此,输入代码:

#getting the file
file = request.files['file']

#reading into pandas df
data = pd.read_excel(file)

df_to_records = data.to_records(index=False)

records_to_bytes = df_to_records.tobytes()

block_blob_service = BlockBlobService(account_name='xxx', account_key="xxx")

block_blob_service.create_blob_from_bytes("test","mydata.csv",records_to_bytes)

谢谢您的任何建议!

1 个答案:

答案 0 :(得分:1)

更新:完整的代码在我这边工作。

import os
from flask import Flask, request, redirect, url_for
from azure.storage.blob import BlockBlobService
import string, random, requests

app = Flask(__name__, instance_relative_config=True)

account = "your_account name"   # Azure account name
key = "account key"      # Azure Storage account access key  
container = "f22" # Container name

blob_service = BlockBlobService(account_name=account, account_key=key)

@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        file = request.files['file']
        file.seek(0)
        filename = "test1.csv" # just use a hardcoded filename for test
        blob_service.create_blob_from_stream(container, filename, file)
        ref =  'http://'+ account + '.blob.core.windows.net/' + container + '/' + filename
        return '''
        <!doctype html>
        <title>File Link</title>
        <h1>Uploaded File Link</h1>
        <p>''' + ref + '''</p>
        <img src="'''+ ref +'''">
        '''

    return '''
    <!doctype html>
    <title>Upload new File</title>
    <h1>Upload new File</h1>
    <form action="" method=post enctype=multipart/form-data>
      <p><input type=file name=file>
         <input type=submit value=Upload>
    </form>
    '''
if __name__ == '__main__':
    app.run(debug=True)

运行后:

enter image description here

选择.csv文件并单击“上传”按钮后,在Azure门户中检查.csv文件:

enter image description here


我认为您可以尝试使用方法create_blob_from_stream代替create_blob_from_bytes

这是示例代码:

def upload_file():    
    if request.method == 'POST':    
        file = request.files['file']
        file.seek(0)                
        try: 
            blob_service = BlockBlobService(account_name='xxx', account_key="xxx")   
            blob_service.create_blob_from_stream(container, filename, file)    
        except Exception:    
            print 'Exception=' + Exception     
            pass