小编use*_*330的帖子

将 xlsx 从 azure blob 存储读取到 pandas dataframe,而不创建临时文件

我正在尝试将 xlsx 文件从 Azure blob 存储读取到 pandas 数据帧,而不创建临时本地文件。我见过很多类似的问题,例如Issues Reading Azure Blob CSV Into Python Pandas DF,但尚未设法使建议的解决方案发挥作用。

下面的代码片段会导致UnicodeDecodeError: 'utf-8' codec can't decode byte 0x87 in position 14: invalid start byte异常。

from io import StringIO
import pandas as pd
from azure.storage.blob import BlobClient, BlobServiceClient

blob_client = BlobClient.from_blob_url(blob_url = url + container + "/" + blobname, credential = token)   
blob = blob_client.download_blob().content_as_text()   
df = pd.read_excel(StringIO(blob))
Run Code Online (Sandbox Code Playgroud)

使用临时文件,我确实设法使其与以下代码片段一起工作:

blob_service_client = BlobServiceClient(account_url = url, credential = token)
blob_client = blob_service_client.get_blob_client(container=container, blob=blobname)

with …
Run Code Online (Sandbox Code Playgroud)

python azure pandas

8
推荐指数
1
解决办法
4717
查看次数

python csv到字典列

是否可以将数据从csv文件读取到字典中,以便一列的第一行是键,而同一列的其余行构成该值的列表?

例如,我有一个csv文件

     strings, numbers, colors 
     string1, 1, blue
     string2, 2, red
     string3, 3, green
     string4, 4, yellow
Run Code Online (Sandbox Code Playgroud)

使用

with open(file,'rU') as f: 
    reader = csv.DictReader(f)
    for row in reader:
        print row
Run Code Online (Sandbox Code Playgroud)

我得到

{'color': 'blue', 'string': 'string1', 'number': '1'}
{'color': 'red', 'string': 'string2', 'number': '2'}
{'color': 'green', 'string': 'string3', 'number': '3'}
{'color': 'yellow', 'string': 'string4', 'number': '4'}
Run Code Online (Sandbox Code Playgroud)

或使用

 with open(file,'rU') as f: 
        reader = csv.reader(f)
        mydict = {rows[0]:rows[1:] for rows in reader}
        print(mydict)
Run Code Online (Sandbox Code Playgroud)

我得到以下字典

{'string3': ['3', 'green'], 'string4': ['4', 'yellow'], 'string2': …
Run Code Online (Sandbox Code Playgroud)

python csv dictionary

2
推荐指数
1
解决办法
5298
查看次数

标签 统计

python ×2

azure ×1

csv ×1

dictionary ×1

pandas ×1