簡體   English   中英

使用 ElementTree (python) 編輯后將修改后的 XML 文件上傳到谷歌雲存儲

[英]Upload a modified XML file to google cloud storage after editting it with ElementTree (python)

我修改了一段代碼,用於將兩個或多個 xml 文件合並為一個。 我讓它在本地工作,而無需在谷歌雲存儲上使用或存儲文件。

我想通過雲功能使用它,除了將最終的 xml 文件上傳到谷歌雲存儲之外,這似乎大部分工作正常。

import os
import wget
import logging

from io import BytesIO
from google.cloud import storage
from xml.etree import ElementTree as ET

def merge(event, context):
    client = storage.Client()
    bucket = client.get_bucket('mybucket')
    test1 = bucket.blob("xml-file1.xml")
    inputxml1 = test1.download_as_string()
    root1 = ET.fromstring(inputxml1)
    test2 = bucket.blob("xml-file2.xml")
    inputxml2 = test2.download_as_string()
    root2 = ET.fromstring(inputxml2)
    copy_files = [e for e in root1.findall('./SHOPITEM')]
    src_files = set([e.find('./SHOPITEM') for e in copy_files])
    copy_files.extend([e for e in root2.findall('./SHOPITEM') if e.find('./CODE').text not in src_files])
    files = ET.Element('SHOP')
    files.extend(copy_files)
    blob = bucket.blob("test.xml")
    blob.upload_from_string(files)

我嘗試了 functions.write 和 .tostring 但沒有成功。

抱歉,問題不完整。 我已經找到了解決方案,但我不記得收到的錯誤消息。 這是我的解決方案:

blob.upload_from_string(ET.tostring(files, encoding='UTF-8',xml_declaration=True, method='xml').decode('UTF-8'),content_type='application/xml')

暫無
暫無

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

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