我在一个文本文件中保存了这种格式的unicode字符串 b'\x1e\x80E\xd7\xd4M\x94\xa8\xb4\xf3bl[^' 但是当我从这个外部文本文件中读取它时,它会作为一个普通的字符串被读取。你知道吗
我尝试过以二进制形式读取文件,例如 打开(celesi文件路径,“rb”)
fciphertext = open(ciphertext_file_path, "rb")
fkey = open(celesi_file_path,"rb")
celesi = fkey.read()
ciphertext = fciphertext.read()
ciphertext = ciphertext.decode('latin-1')
celesi = celesi.decode('latin-1')
print(type(celesi))
print(type(ciphertext))
print(celesi)
print(ciphertext)
输出为字符串: “b'\x1e\x80E\xd7\xd4M\x94\xa8\xb4\xf3bl[^'” 我希望它是一个字符串,而不是这种格式
看看这个:
所以,如果你写
str(data)
到文件,你写的是斜杠和x
。您没有编写字节,而是编写了Python提供的这些字节的字符串表示法。在本例中,您编写了51字节(!)而不是原来的12。你知道吗您应该自己编写字节:
然后以二进制模式打开这个文件并读取字节。你知道吗
相关问题 更多 >
编程相关推荐