Error in using `struct.pack` for writing data to file

有些话、适合烂在心里 提交于 2019-12-01 05:40:35

问题


I have a numpy.ndarray sample of numbers, each between 1 and 2**20.

I'd like to write it into a binary file, such that each element is represented by four bytes.

However, the resulting file size is different from 4 times the size of the sample.

This is the code I'm using:

        outputFile = open('testDS', 'w')
        print len(sample)
        if (outputFile is not None):
            for s in sample:
                assert(s < 2**20)
                r = struct.pack("i", s)
                assert(len(r) == 4)
                outputFile.write(r)
        outputFile.close()

The output I'm getting (the size of the sample) is: 1000

However, the resulting file size is 4026 bytes.

Any ideas why the file size is not exactly 4000 bytes?


回答1:


Open the file in binary mode:

outputFile = open('testDS', 'wb')

Otherwise, the file object may do some magic translation of newline characters that show up in your binary data, resulting in additional characters being written to the file. See, for example, https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files



来源:https://stackoverflow.com/questions/33333284/error-in-using-struct-pack-for-writing-data-to-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!