Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python将XML转换为base64_Python_Xml_Base64 - Fatal编程技术网

使用python将XML转换为base64

使用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:

如何使用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'))

XML仍然是一个字符串。“但是它需要一个字符串(像字节)”-不要
打开('file.XML','r')
,要
打开('file.XML','rb')
,非常感谢@亚历山德拉·杜德基纳