【发布时间】:2014-03-14 10:39:03
【问题描述】:
问题是如何将从base64解码的字符串写入文件?我使用下一段代码:
import base64
input_file = open('Input.txt', 'r')
coded_string = input_file.read()
decoded = base64.b64decode(coded_string)
output_file = open('Output.txt', 'w')
output_file.write(decoded)
output_file.close()
Input.txt 包含 base64 字符串(smth. like PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48cmV2aW)。脚本执行后,我想在 Output.txt 中看到 xml,但输出文件包含一些错误的符号(如 <?xml version="1.0" encoding="UTF-8"?><review-case create®vFFSТ#2)。同时,如果我没有从文件 Input.txt 中读取 base64 字符串,而是在脚本中将其指定为 coded_string = '''PD94bWwgdmVyc2lvbj0iMS4wIiBlbm...''',则 Output.txt 包含正确的 xml。这是utf编码有问题吗?如何解决这个问题?我在 Windows 7 上使用 Python2.7。提前致谢。
【问题讨论】:
-
顺便说一句,它在我的 Ubuntu 上的行为方式相同。