簡體   English   中英

如何使用來自 Azure StorageStreamDownloader 的 lxml iterparse?

[英]how to use lxml iterparse from Azure StorageStreamDownloader?

我目前正在使用lxml.etree.iterparse逐個標記迭代 XML 文件標記。 Locally this works fine but I want to move the XML file to an Azure Blob Storage and process the file in an Azure function. 但是,我有點堅持嘗試從StorageStreamDownloader解析 XML 文件

本地編碼

from lxml import etree

context = etree.iterparse('c:\\Users\\', tag='InstanceElement')

for event, elem in context:
    # processing of the tag

從 Blob 流式傳輸

from lxml import etree
from azure.storage.filedatalake import DataLakeServiceClient

connect_str = ''
service = DataLakeServiceClient.from_connection_string(conn_str=connect_string)

System = service.get_file_system_client('')
FileClient = System.get_file_client('')
Stream = FileClient.download_file()

# Stuck on what the input must be for iterparse
context = etree.iterparse(, tag='InstanceElement')

for event, elem in context:
    # processing of the tag

我堅持iterparse的輸入必須是什么,所以關於如何在流式傳輸時解析 XML 文件的任何想法?

嘗試這個:

from lxml import etree
from azure.storage.filedatalake import DataLakeServiceClient
from io  import BytesIO

connect_str = ''
service = DataLakeServiceClient.from_connection_string(conn_str=connect_str)

System = service.get_file_system_client('')
FileClient = System.get_file_client('test.xml')
content = FileClient.download_file().readall()

context = etree.iterparse(BytesIO(content), tag='InstanceElement')
for event, elem in context:
    print(elem.text)

我的test.xml的內容:

在此處輸入圖像描述

結果:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM