简体   繁体   English

使用 Python 解析 Azure XML BLOB

[英]Parse Azure XML BLOB using Python

Trying to parse an XML BLOB and convert it into CSV. Able to use the following code when using a Local File.尝试解析 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 = []

Not able to use when working on Azure BLOB.在处理 Azure BLOB 时无法使用。 How can I do that?我怎样才能做到这一点? Not the cleanest but tried the following way by creating the URL with parameters.不是最干净的,但通过使用参数创建 URL 尝试了以下方法。 The Container is set for Public access and Blobs don't have restrictions.容器设置为公共访问,Blob 没有限制。 Library used: azure-storage-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 = []

Any Suggestion to make it work?有什么建议可以让它发挥作用吗? Better way to access Blob?访问 Blob 的更好方法?

If you want to read xml file from Azure blob, we can use package azure.storage.blob to implement it.如果要从Azure blob中读取xml文件,我们可以使用package azure.storage.blob来实现。

For example例如

My xml file我的 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>
  1. Code代码
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)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM