使用python将XML转换为base64
如何使用python/scala将完整的xml文件转换为base64字符串 我已经尝试了b64模块,但它需要向其传递一个字符串(类似字节)。但是,考虑到ML的多行结构和层次结构,如何使用它来实现这一点。 谁能举个例子说明怎么做 谢谢。Python解决方案:使用python将XML转换为base64,python,xml,base64,Python,Xml,Base64,如何使用python/scala将完整的xml文件转换为base64字符串 我已经尝试了b64模块,但它需要向其传递一个字符串(类似字节)。但是,考虑到ML的多行结构和层次结构,如何使用它来实现这一点。 谁能举个例子说明怎么做 谢谢。Python解决方案: import base64 # convert file content to base64 encoded string with open("input.xml", "rb") as file:
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'))
XML仍然是一个字符串。“但是它需要一个字符串(像字节)”-不要
打开('file.XML','r')
,要打开('file.XML','rb')
,非常感谢@亚历山德拉·杜德基纳