如何使用 python 和 pandas read_fwf 函数处理位于 Azure blob 存储中的文件

How to process a file located in Azure blob Storage using python with pandas read_fwf function

我需要使用 python 打开并处理文本文件中的数据。 该文件将存储在 Azure Blob 存储或 Azure 文件共享中。

但是,我的问题是我可以使用与 windows 中使用的 os.chdir() 和 read_fwf() 相同的模块和函数吗?我想要的代码 运行:

import pandas as pd
import os
os.chdir( file_path)
df=pd.read_fwf(filename)

我希望能够 运行 此代码并且 file_path 将成为 Azure blob 中的一个目录。

如果可能,请告诉我。如果您对文件的存储位置有更好的了解,请分享。

谢谢,

据我所知,os.chdir(path)只能对本地文件进行操作。如果想将文件从存储移动到本地,可以参考以下代码:

    connect_str = "<your-connection-string>"
    blob_service_client = BlobServiceClient.from_connection_string(connect_str)
    container_name = "<container-name>"
    file_name = "<blob-name>"
    container_client = blob_service_client.get_container_client(container_name)
    blob_client = container_client.get_blob_client(file_name)
    download_file_path = "<local-path>"
    with open(download_file_path, "wb") as download_file:
        download_file.write(blob_client.download_blob().readall())

pandas.read_fwf 可以使用 URL:

直接从存储中读取 blob

例如:

    url = "https://<your-account>.blob.core.windows.net/test/test.txt?<sas-token>"
    df=pd.read_fwf(url)