XML 使用 python 到 base64

XML to base64 using python

如何使用 python/scala 将完整的 xml 文件转换为 base64 字符串?

我试过 b64 模块,但它需要一个字符串(类似字节)传递给它。但是考虑到它的多行结构和层次结构,如何使用 ML 做到这一点。 谁能举例说明如何做。

谢谢。

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'))