所以我收到了这样的请求调用的响应:
resourceType": "Binary",
"id": "07a6483f-732b-461e-86b6-edb665c45510",
"contentType": "application/msword",
"content": "UEsDBBQA.......
它在我的代码中显示为:
b'<Binary xmlns="http://hl7.org/fhir">\r\n <id value="07a6483f-732b-461e-86b6-edb665c45510"></id>\r\n <contentType value="application/msword"></contentType>\r\n <content value="UEsDBBQ
我需要提取内容值并另存为,在本例中为 Word 文档。我尝试过基于拆分和使用 ElementTree 的解决方案,但我似乎无法解析内容并将其保存为 word 文件。
现在就这样做:
with open('/tmp/metadata.doc', 'wb') as f:
f.write(response.content
结果是写了整篇文章,Word 会像这样打开:
有什么想法吗?提前致谢
答案 0 :(得分:0)
鉴于 response.content
是一个字符串变量,可以使用 ElementTree 解析 XML:
from xml.etree import ElementTree as etree
# declare namespace
ns = {'ns': 'http://hl7.org/fhir'}
# parse XML
tree = etree.fromstring(response.content)
# find content element considering namespace
content_element = tree.find('.//ns:content', ns)
# extract attribute "value"
content = content_element.get('value')
# save content to file