简体   繁体   English

如何读取文件,将二进制缓冲区转换为二进制字符串,然后在 python 中再次将二进制字符串转换为二进制缓冲区?

[英]How to read file, convert binary buffer to binary string and later convert the binary string to binary buffer again in python?

I opened a file (.jpeg) in binary format and stored it's content in a variable and converted the binary buffer into string using str().我以二进制格式打开了一个文件(.jpeg)并将其内容存储在一个变量中,并使用 str() 将二进制缓冲区转换为字符串。 Now, I want to convert the string into binary buffer again.现在,我想再次将字符串转换为二进制缓冲区。

from tkinter import filedialog
file_path = filedialog.askopenfilename()
file = open(file_path,"rb")
file_content = file.read()
file.close()
print(file_content)
file_content_str = str(file_content)
print(file_content)

# want a code to convert file_content_str into bytes again here
# file_content_bytes = file_content_string converted to bytes

# file2 = open("moon2.jpg", "w+b")
# file2.write(file_content_bytes)
# file2.close()

As much as I avoid eval, try this:尽我所能避免评估,试试这个:

file_content_bytes = eval(file_content_str)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM