您好,欢迎来到暴趣科技网。
搜索
您的当前位置:首页python生成dat文件,python中的.dat文件

python生成dat文件,python中的.dat文件

来源:暴趣科技网

I'am doing a project in python using OpenCV. I have to store a large amount of integer data(features of images in the database) in a separate file. I can use .txt file but it stores integer values as strings.

Is there any way that I can store integer values directly as integers in python like .dat file in MATLAB.?

解决方案

You can use struct to pack the integers in a bytes format and write them to a dat file.

With integers, this will result in a file that contains 4 bytes per integer, which would save a bit of space (over text format) if you have very large numbers. If you have smaller numbers, a csv format may be better.

import struct

data = [1,2,3,4,5,6,7,8,9]

with open('data.dat', 'wb') as data_file:

data_file.write(struct.pack('i'*len(data), *data))

Then to read it back in

with open('data.dat', 'rb') as data_file:

values = struct.unpack('i'*len(data), data_file.read())

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- baoquwan.com 版权所有 湘ICP备2024080961号-7

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务