简体   繁体   English

Python在磁盘上的文件中存储二进制数据

[英]Python Storing a Binary Data in File on disk

Wondering what would be a good choice to store a binary data in file on a disk. 想知道将二进制数据存储在磁盘上的文件中的最佳选择是什么。 Great if it would be a built-in Python module since I would like to keep it all stock. 如果它将是内置的Python模块,那就太好了,因为我想保留所有库存。 A random access to a written data could be a plus but it's not required. 可以随机访问书面数据,但这不是必须的。 For this implementation I would rather go for a simplicity and speed. 对于此实现,我宁愿追求简单性和速度。 What I am looking for: save it now - get it later. 我正在寻找的是:现在保存-以后再获取。 Thanks in advance! 提前致谢!

EDITED: 编辑:

Found a problem why cPickle was erroring out. 发现了一个问题,为什么cPickle出错了。 In one of the classes I have declared self.os=os And it appears self.os is not something cPickle likes... A bit later I found that cPickle doesn't accept PyQT objects if they (PyQT class instances) are given as a class's attributes (in case you dump a list of some_class instances). 在其中一个类中,我声明了self.os = os,似乎self.os不是cPickle喜欢的东西……稍后,我发现cPickle不接受以下给定的PyQT对象(PyQT类实例):类的属性(以防万一您转载some_class实例的列表)。

An example below if run replicates the same error: 下面的示例if run复制相同的错误:

import cPickle
import os

class MyClass(object):
    """docstring for MyClass"""
    def __init__(self, arg):
        super(MyClass, self).__init__()
        self.arg = arg
        self.os=os         

data=MyClass("Hello World")    

file_name='dampData.data'

out_file = open(file_name, 'wb')
cPickle.dump(data, out_file)
out_file.close()

I would recommend cPickle - it is also built-in and significantly faster than pickle (in most cases). 我会推荐cPickle-它也是内置的,并且比pickle快得多(在大多数情况下)。

example: 例:

import cPickle

out_file = open(file_name, 'w')
cPickle.dump(data, out_file)
out_file.close()

in_file = open(file_name, 'r')
data = cPickle.load(in_file)
in_file .close()

From the official documentation of pickle : 泡菜的官方文档中:

The pickle module has an optimized cousin called the cPickle module. 泡菜模块具有称为cPickle模块的优化表弟。 As its name implies, cPickle is written in C, so it can be up to 1000 times faster than pickle. 顾名思义,cPickle用C编写,因此它的速度可以比pickle快1000倍。

看看泡菜货架模块!

You can use the normal python functions to read/write binary. 您可以使用普通的python函数读取/写入二进制文件。 Add a 'b' to the mode if on Windows: 如果在Windows上,请在模式中添加“ b”:

f = open('workfile', 'wb') # opens a file for writing in binary mode

If you're using python 3, then you may need to do a little more work with string encodings. 如果您使用的是python 3,则可能需要对字符串编码做更多的工作。

Reading and Writing Files: 读写文件:

http://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files http://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files

You may also want to serialize your data so its easier to work with 您可能还希望序列化数据,以便更轻松地使用它

http://docs.python.org/2/library/pickle.html http://docs.python.org/2/library/pickle.html

It's builtin. 它是内置的。

f = open("somefile.zip", "rb")
g = open("thecopy.zip", "wb")

while True:
    buf = f.read(1024)
    if len(buf) == 0:
         break
    g.write(buf)

f.close()
g.close()

http://openbookproject.net/thinkcs/python/english3e/files.html http://openbookproject.net/thinkcs/python/english3e/files.html

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

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