python struct使用

python struct模块

主要内容来自转自:http://blog.163.com/sywxf@126/blog/static/50671196201011319507710/

 

简单读一个二进制文件:

 
# coding: utf-8
 
import struct
import sys
binfile=open('test.bin','rb')
buf = binfile.read()
unpackIndex = 0
a,b,c,d = struct.unpack_from('ifdh', buf, unpackIndex)
print a, b, c, d
unpackIndex += struct.calcsize('ifdh')

 

此文件存储一个结构体

struct abc{

int a;

float b;

double c;

short d;

};

 

c++代码为:

// writeandreadbin.cpp : 定义控制台应用程序的入口点。
//
 
#include "stdafx.h"
 
 
#include<fstream>
#include <iostream>
using namespace std;
void main()
{
    ofstream fwrite("test.bin", ios::out | ios::app | ios::binary);
    if(!fwrite){
        cout<<"File open error!\n";
        return;
    }
 
    int a = 0x12345678;
    float b = 3.25;
    double c = 4.33;
    short d = 0x2468;
    fwrite.write((char*)&a, sizeof(a));
    fwrite.write((char*)&b, sizeof(b));
    fwrite.write((char*)&c, sizeof(c));
    fwrite.write((char*)&d, sizeof(d));
 
    fwrite.close();
 
    ifstream fread("test.bin", ios::in|ios::binary);
    if(!fread)
    {
        cout<<"File open error!"<<endl;
        return;
    }
    fread.read((char*)&a, sizeof(a));
    fread.read((char*)&b, sizeof(b));
    fread.read((char*)&c, sizeof(c));
    fread.read((char*)&d, sizeof(d));
    fread.close();
 
    cout<<a<<endl;
    cout<<b<<endl;
    cout<<c<<endl;
    cout<<d<<endl;
}

 

python用以做一些简单的结构文件的读取还是很不错的,如下读取的二进制文件,显示出相关字段值:

#!usr/bin/env python
# coding: utf-8
# read_codefilelist.py
 
'''
typedef struct SecuCodeNewFileItem
{
    unsigned int    SecuCode;       // 证券代码
    unsigned char    SecuName[20];   // 证券名称,
    unsigned int    MarketType;     // 新市场类型
    signed char        Jianpin[8];
    float            PervClose;      // 昨收
    float            Last5AV;        // 过去5日平均每分钟成交量,用于计算量比
    float            Circulation;    // 流通盘
}structSecuCodeNewFileItem;
'''
import struct
import sys
 
type = sys.getfilesystemencoding()
 
binfile=open('codelist_pad.dat','rb')
buf = binfile.read()
unpackIndex = 0
count = struct.unpack_from('i', buf, unpackIndex)
unpackIndex += struct.calcsize('i')
print 'count=', count[0]
 
for i in range(0, count[0], 1):
    SecuCode,\
    SecuName,    \
    MarketType,  \
    Jianpin, \
    PervClose, \
    Last5AV, \
    Circulation = struct.unpack_from('i20si8sfff', buf, unpackIndex)
    
    print '%06d'%SecuCode, MarketType, \
          SecuName.decode(encoding='UTF-8', errors='ignore').rstrip('\000')
 
    unpackIndex += struct.calcsize('i20si8sfff')
    
 
name = input("Please input any key to exit:\n")

 

后来想了一下,前面的写法还是有点麻烦,用python目的就是为了快,前面的写法在需要改结构的时候,显得很不方便,以下更快一点:

#!usr/bin/env python
# coding: utf-8
# read_codefilelist.py
 
'''
typedef struct SecuCodeNewFileItem
{
    unsigned int    SecuCode;       // 证券代码
    unsigned char    SecuName[20];   // 证券名称,
    unsigned int    MarketType;     // 新市场类型
    signed char        Jianpin[8];
    float            PervClose;      // 昨收
    float            Last5AV;        // 过去5日平均每分钟成交量,用于计算量比
    float            Circulation;    // 流通盘
}structSecuCodeNewFileItem;
'''
import struct
import sys
 
type = sys.getfilesystemencoding()
 
binfile=open('codelist_pad.dat','rb')
buf = binfile.read()
unpackIndex = 0
count = struct.unpack_from('i', buf, unpackIndex)
unpackIndex += struct.calcsize('i')
print 'count=', count[0]
 
for i in range(0, count[0], 1):
    data = struct.unpack_from('i20si8sfff', buf, unpackIndex)
    print '%06d'%data[0], data[2],\
          data[1].decode(encoding='UTF-8', errors='ignore').rstrip('\000')
    
    unpackIndex += struct.calcsize('i20si8sfff')
    
    
name = input("Please input any key to exit:\n")
posted @ 2012-05-10 23:03  绿色的麦田  阅读(836)  评论(0编辑  收藏  举报