我修改了一段代码,用于将两个或多个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)

我试过这些功能.写作和写作.tostring但没有成功.

推荐答案

抱歉,问题不完整.我已经找到了解决方案,我记不起收到的错误消息.

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

Python相关问答推荐

实现的差异取决于计算出的表达是直接返回还是首先存储在变量中然后返回

如何在Python中使用时区夏令时获取任何给定本地时间的纪元值?

通过优化空间在Python中的饼图中添加标签

使用SciPy进行曲线匹配未能给出正确的匹配

Django管理面板显示字段最大长度而不是字段名称

在Python Attrs包中,如何在field_Transformer函数中添加字段?

如何在Django基于类的视图中有效地使用UTE和RST HTIP方法?

OR—Tools中CP—SAT求解器的IntVar设置值

在Python argparse包中添加formatter_class MetavarTypeHelpFormatter时, - help不再工作""""

从spaCy的句子中提取日期

driver. find_element无法通过class_name找到元素'""

如何使用Numpy. stracards重新编写滚动和?

使用BeautifulSoup抓取所有链接

Pandas—堆栈多索引头,但不包括第一列

Python pint将1/华氏度转换为1/摄氏度°°

Pandas—MultiIndex Resample—我不想丢失其他索引的信息´

如何在FastAPI中替换Pydantic的constr,以便在BaseModel之外使用?'

GPT python SDK引入了大量开销/错误超时

freq = inject在pandas中做了什么?''它与freq = D有什么不同?''

如何在Python中创建仅包含完整天数的月份的列表