为了账号安全,请及时绑定邮箱和手机立即绑定

使用 Python 解析 Azure XML BLOB

使用 Python 解析 Azure XML BLOB

牧羊人nacy 2023-06-13 11:02:35
尝试解析 XML BLOB 并将其转换为 CSV。使用本地文件时可以使用以下代码。import xml.etree.ElementTree as etSourceFileName = req.params.get('FileName')SourceContainer = "C:\\AzureInputFiles\\"SourceFileFullPath = SourceContainer + SourceFileNamextree = et.parse(SourceFileFullPath)xroot = xtree.findall(".//data/record") df_cols=['Col1', 'Col2']rows = []在 Azure BLOB 上工作时无法使用。我怎样才能做到这一点 ?不是最干净的,但通过创建带参数的 URL 尝试了以下方式。容器设置为公共访问,Blob 没有限制。使用的库:azure-storage-blobimport xml.etree.ElementTree as eturl = f"https://{account_name}.blob.core.windows.net/{container_name}/{blob_name}"xtree = et.parse(url)xroot = xtree.findall(".//data/record") df_cols=['Col1', 'Col2']rows = []有什么建议可以让它发挥作用吗?访问 Blob 的更好方法?
查看完整描述

1 回答

?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

如果你想从 Azure blob 中读取 xml 文件,我们可以使用 packageazure.storage.blob来实现它。


例如


我的 xml 文件


<?xml version="1.0"?>

<data>

    <country name="Liechtenstein">

        <rank>1</rank>

        <year>2008</year>

        <gdppc>141100</gdppc>

        <neighbor name="Austria" direction="E"/>

        <neighbor name="Switzerland" direction="W"/>

    </country>

    <country name="Singapore">

        <rank>4</rank>

        <year>2011</year>

        <gdppc>59900</gdppc>

        <neighbor name="Malaysia" direction="N"/>

    </country>

    <country name="Panama">

        <rank>68</rank>

        <year>2011</year>

        <gdppc>13600</gdppc>

        <neighbor name="Costa Rica" direction="W"/>

        <neighbor name="Colombia" direction="E"/>

    </country>

</data>

代码

import xml.etree.ElementTree as ET


from azure.storage.blob import BlobServiceClient


connection_string='<your storage account connection string>'

blob_service_client = BlobServiceClient.from_connection_string(connection_string)


blob_client = blob_service_client.get_blob_client(container="test", blob="data.xml")

downloader = blob_client.download_blob()

root = ET.fromstring(downloader.content_as_text())

for neighbor in root.iter('neighbor'):

    print(neighbor.attrib)

查看完整回答
反对 回复 2023-06-13
  • 1 回答
  • 0 关注
  • 73 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信