简体   繁体   English

XML 到 base64 使用 python

[英]XML to base64 using python

How to convert complete xml file to base64 string using python/ scala?如何使用 python/scala 将完整的 xml 文件转换为 base64 字符串?

I have tried b64 module,but it requires a string(bytes-like) to be passed to it.我已经尝试过 b64 模块,但它需要一个字符串(类似字节)传递给它。 But how to do that with ML given it's multiline structure and hierarchy.但是考虑到它的多行结构和层次结构,如何使用 ML 来做到这一点。 Could anyone give an example on how to do it.任何人都可以举一个例子来说明如何做到这一点。

Thanks.谢谢。

Python solution: Python解决方案:

import base64

# convert file content to base64 encoded string
with open("input.xml", "rb") as file:
    encoded = base64.encodebytes(file.read()).decode("utf-8")

# output base64 content    
print(encoded)

decoded = base64.decodebytes(encoded.encode('utf-8'))

# write decoded base64 content to file
with open("output.xml", "wb") as file:
    file.write(decoded)

# output decoded base64 content
print(decoded.decode('utf-8'))

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

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