1

How to convert complete xml file to base64 string using python/ scala?

I have tried b64 module ,but it requires a string(bytes-like) to be passed to it . But how to do that with ML given it's multiline structure and hierarchy. Could anyone give an example on how to do it.

Thanks.

2
  • XML is still a string. Commented Nov 25, 2020 at 19:08
  • "but it requires a string(bytes-like)" - don't open('file.xml', 'r'), do open('file.xml', 'rb') Commented Nov 25, 2020 at 20:04

1 Answer 1

1

Python solution:

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'))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.