使用 Python 解析 Azure XML BLOB

尝试解析 XML BLOB 并将其转换为 CSV。使用本地文件时可以使用以下代码。


import xml.etree.ElementTree as et


SourceFileName = req.params.get('FileName')

SourceContainer = "C:\\AzureInputFiles\\"

SourceFileFullPath = SourceContainer + SourceFileName


xtree = et.parse(SourceFileFullPath)

xroot = xtree.findall(".//data/record") 

df_cols=['Col1', 'Col2']

rows = []

在 Azure BLOB 上工作时无法使用。我怎样才能做到这一点 ?不是最干净的,但通过创建带参数的 URL 尝试了以下方式。容器设置为公共访问,Blob 没有限制。使用的库:azure-storage-blob


import xml.etree.ElementTree as et


url = 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 的更好方法?


牧羊人nacy
浏览 74回答 1
1回答

摇曳的蔷薇

如果你想从 Azure blob 中读取 xml 文件,我们可以使用 packageazure.storage.blob来实现它。例如我的 xml 文件<?xml version="1.0"?><data>&nbsp; &nbsp; <country name="Liechtenstein">&nbsp; &nbsp; &nbsp; &nbsp; <rank>1</rank>&nbsp; &nbsp; &nbsp; &nbsp; <year>2008</year>&nbsp; &nbsp; &nbsp; &nbsp; <gdppc>141100</gdppc>&nbsp; &nbsp; &nbsp; &nbsp; <neighbor name="Austria" direction="E"/>&nbsp; &nbsp; &nbsp; &nbsp; <neighbor name="Switzerland" direction="W"/>&nbsp; &nbsp; </country>&nbsp; &nbsp; <country name="Singapore">&nbsp; &nbsp; &nbsp; &nbsp; <rank>4</rank>&nbsp; &nbsp; &nbsp; &nbsp; <year>2011</year>&nbsp; &nbsp; &nbsp; &nbsp; <gdppc>59900</gdppc>&nbsp; &nbsp; &nbsp; &nbsp; <neighbor name="Malaysia" direction="N"/>&nbsp; &nbsp; </country>&nbsp; &nbsp; <country name="Panama">&nbsp; &nbsp; &nbsp; &nbsp; <rank>68</rank>&nbsp; &nbsp; &nbsp; &nbsp; <year>2011</year>&nbsp; &nbsp; &nbsp; &nbsp; <gdppc>13600</gdppc>&nbsp; &nbsp; &nbsp; &nbsp; <neighbor name="Costa Rica" direction="W"/>&nbsp; &nbsp; &nbsp; &nbsp; <neighbor name="Colombia" direction="E"/>&nbsp; &nbsp; </country></data>代码import xml.etree.ElementTree as ETfrom azure.storage.blob import BlobServiceClientconnection_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'):&nbsp; &nbsp; print(neighbor.attrib)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python