0

使用下面的代码,我已成功读取文本文件并将其转换为 Python 中的 Base85。但是,我注意到每一行都是这样写的b'Rr(hAR(hX=Wf{sjSbZv'

import base64

file_name = "textfile.txt"

with open(file_name, "r") as in_file:
    with open("b85_encoded.txt", "w")  as out_file:
        for line in in_file:
            enc_line = base64.b85encode(bytes(line, "utf-8"))
            out_file.write(str(enc_line))

当我想解码文件b85_encoded.txt时,我得到了这个错误ValueError: bad base85 character at position 1

import base64

file_name = "b85_encoded.txt"

with open(file_name, "r") as in_file:
    with open("b85_decoded.txt", "w") as out_file:
        for line in in_file:
            dec_line = base64.b85decode(bytes(line,"utf-8"))
            out_file.write(str(dec_line))

在写入和读取文件时如何正确编码和解码?我可以正确编码和解码而无需写入和读取文件..所以看来我在那里做错了。

非常感谢您的帮助。

4

0 回答 0