[英]how to store Hex data using json in python
I am getting this error when storing data in to a file 将数据存储到文件中时出现此错误
TypeError: b'\\x1fa\\xec\\xb5\\xed]k\\xaf\\x8dzph\\xb2\\x8d\\xcc\\x8e' is not JSON serializable TypeError:b'\\ x1fa \\ xec \\ xb5 \\ xed] k \\ xaf \\ x8dzph \\ xb2 \\ x8d \\ xcc \\ x8e'不可序列化JSON
This is the code I am using. 这是我正在使用的代码。 But it doesn't seem to be giving the right outcome as the file gets corrupted once the data is stored. 但这似乎并没有给出正确的结果,因为一旦存储了数据,文件就会损坏。
iv = os.urandom (16)
msg_IV = base64.b64encode(iv)
padding = u'\u0000'
file = {"IV": msg_IV,\
"Padding Character": padding }
fp = open("local.txt", "w")
json.dump(file, fp, indent=4)
fp.close()
This small program is supposed to store encrypted variable information and store in a file. 这个小程序应该存储加密的变量信息并存储在文件中。
What you have is a byte string, and there is no standard way to store raw bytes in a JSON object. 您所拥有的是一个字节字符串,并且没有标准方法将原始字节存储在JSON对象中。
You should instead encode the bytes into base64 and store the base64 string in your JSON object: 您应该将字节编码为base64并将base64字符串存储在JSON对象中:
import base64
b64String = base64.b64encode(myByteString)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.