python struct 사용
10691 단어 python
주요 내용
간단하게 바 이 너 리 파일 읽 기:
# 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!
"; 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:
") 나중에 생각해 보 니 앞의 문법 은 좀 번 거 롭 습 니 다. 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:
")
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
로마 숫자를 정수로 또는 그 반대로 변환그 중 하나는 로마 숫자를 정수로 변환하는 함수를 만드는 것이었고 두 번째는 그 반대를 수행하는 함수를 만드는 것이었습니다. 문자만 포함합니다'I', 'V', 'X', 'L', 'C', 'D', 'M' ; 문자열이 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.