亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 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 關注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號