Using Python, how do I to read/write data in memor

2019-01-26 06:02发布

I'm used to C++, and I build my data handling classes/functions to handle stream objects instead of files. I'd like to know how I might modify the following code, so that it can handle a stream of binary data in memory, rather than a file handle.

def get_count(self):
    curr = self.file.tell()
    self.file.seek(0, 0)
    count, = struct.unpack('I', self.file.read(c_uint32_size))
    self.file.seek(curr, 0)
    return count

In this case, the code assumes self.file is a file, opened like so:

file = open('somefile.data, 'r+b')

How might I use the same code, yet instead do something like this:

file = get_binary_data()

Where get_binary_data() returns a string of binary data. Although the code doesn't show it, I also need to write to the stream (I didn't think it was worth posting the code for that).

Also, if possible, I'd like the new code to handle files as well.

5条回答
时光不老,我们不散
2楼-- · 2019-01-26 06:38

You can use an instance of StringIO.StringIO (or cStringIO.StringIO, faster) to give a file-like interface to in-memory data.

查看更多
戒情不戒烟
3楼-- · 2019-01-26 06:44

Take a look at Python's StringIO module, docs here, which could be pretty much what you're after.

查看更多
甜甜的少女心
4楼-- · 2019-01-26 06:45

I like the timing of the answer. (except mine)

We can see response time in milliseconds ?

of-course StringIO

查看更多
乱世女痞
5楼-- · 2019-01-26 07:00
混吃等死
6楼-- · 2019-01-26 07:01

Have a look at 'StringIO' (Read and write strings as files)

查看更多
登录 后发表回答