python - 在python中将txt文件保存为二进制文件

标签 python file controller odoo

如何生成将保存为二进制文件的 txt 文件。 据说在web controller(odoo)中调用文件需要是二进制

最佳答案

# read textfile into string 
with open('mytxtfile.txt', 'r') as txtfile:
    mytextstring = txtfile.read()

# change text into a binary array
binarray = ' '.join(format(ch, 'b') for ch in bytearray(mytextstring))

# save the file
with open('binfileName', 'br+') as binfile:
    binfile.write(binarray)

关于python - 在python中将txt文件保存为二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45974344/

相关文章:

python - 如何从 Python 中的像素值列表创建 PNG 图像文件?

java - 放入 Java 应用程序时如何更改光标

asp.net-mvc - 提交表单并将数据传递给 FileStreamResult 类型的 Controller 方法

Laravel 获取同类产品

python - 如何在处理过程中删除 "simulate"numpy.delete

python - 使用 Python 查找序列比对中间隙 (indel) 的位置和长度

python - 如何使用 Python 在任意平面内绘制矢量场?

python - 替换 DF 中的单元格,其中 DF 的单元格是其他 DF 中所需行的索引

model-view-controller - 我应该测试我的 Controller (MVC)吗?

file - 我如何要求和提供Clojure文件?