473,385 Members | 1,824 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Can not get struct to read the entire binary file.

I succeed in extracting all of the header information, and then only a portion of the data. The data section is 123,410 doubles, but after retrieving 62 of the numbers, I can not go further, struct.unpack indicates that the string argument is the wrong length. I set up a loop to get the numbers one at a time, and it stops at the same place. The file is not corrupt, and will open in the original software. Does anyone have suggestions for figuring out the problem?
Expand|Select|Wrap|Line Numbers
  1. import struct, string, os
  2.  
  3. bf = "F:/Junker/TestT/TU_TSA.grd"
  4. wbf = open(bf).read()  # "rb")
  5. start, stop = 0, struct.calcsize('<7l8d2l')
  6.  
  7. #Retrieve the header information
  8. try:
  9.     TheadID,TheadSize,HSver,TgridID,TgridSize,nRow,nCol,xLL,yLL,xSize,ySize,zMin,zMax,\
  10.     Rotation,BlankVal,TdataID,TdataSize = struct.unpack('<7l8d2l', wbf[start:stop])
  11. except:
  12.     print "Problem reading Header"
  13. print "TheadID ", TheadID, "\n ", "TheadSize ",TheadSize,"\n ","HSver ", HSver,"\n ","TheadID ",TgridID,"\n ","TgridSize ",TgridSize,"\n ","nRow ",nRow,"\n ","nCol ",nCol,"\n ","xLL ",xLL,"\n ","yLL ",yLL,"\n ","xSize ",xSize,"\n ","ySize ",ySize,"\n ","zMin ",zMin,"\n ","zMax ",zMax,"\n ","Rotation ",Rotation,"\n ","BlankVal ",BlankVal,"\n ","TdataID ",TdataID,"\n ","TdataSize",TdataSize
  14.  
  15. #Retrieve the data
  16. try:
  17.     x = nCol * nRow
  18.     while x:
  19.         start, stop = stop, stop + struct.calcsize('<d')
  20.         DataBlock = struct.unpack('<d', wbf[start:stop])
  21.         print DataBlock, " start ",start, " stop ",stop
  22.  
  23. except:
  24.     print "Failed at: start ",start, " stop ",stop
  25.  
  26. del TheadID,TheadSize,HSver,TgridID,TgridSize,nRow,nCol,xLL,yLL,xSize,ySize,zMin,zMax,\
  27.     Rotation,BlankVal,TdataID,TdataSize,start,stop
  28.  
Nov 3 '10 #1
3 2336
dwblas
626 Expert 512MB
Test the length of "wbf" and see if it is approximately the same size as the file. Also it is not being read in binary mode:
Expand|Select|Wrap|Line Numbers
  1. ##  should be
  2. wbf = open(bf, "rb").read()  # "rb") 
Nov 3 '10 #2
That is just embarrassing, I changed this so many times and tried so many things, that at some point I commented out the read binary mode, and never put it back. Grrrrrr.
Thank you for pointing out what I was too close to see.
Nov 4 '10 #3
dwblas
626 Expert 512MB
The answer is usually embarrassingly simple _after_ you know it.
Nov 4 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Jesse Noller | last post by:
The problem: I am writing a file uploading utility in python that uses the walk() function to parse a directory, finding any file under that directory, and upload it to a remote server using the...
0
by: john_phx | last post by:
I'm trying to increase the performance of a program that concatenates binary file parts into a single file. Each of the parts is contained in a binary file. The existing app simply takes the first...
0
by: Shmurthy | last post by:
I have a binary file. How do i search for a string (pattern) and replace dynamically with new data. Thank Shmurthy
5
by: maxwell | last post by:
I used to program in matlab An read an image file with : fread(a,,0) which gave me a matrix of 256*256 full of numbers which were the pidxel values from 0 to 256. How do i do the same in c++ ...
14
by: spike | last post by:
Im trying to write a program that should read through a binary file searching for the character sequence "\name\" Then it should read the characters following the "\name\" sequence until a NULL...
4
by: chinthamani | last post by:
Hai, I want to read from a particular point of the file not from the beginning of the file. It may be from the second line or the third or from any position, so how is it possible for me to reach...
6
by: cooldisk | last post by:
Is it possible at all to read a binary file larger than 2GB on a 32- bit system? I tried the following: #include <iostream> #include <fstream> using namespace std; int main(int argc, char...
4
by: =?ISO-8859-1?Q?Hans_M=FCller?= | last post by:
Good morning folks, I cannot read a binary file into a mysql database. Everything I tried did not succeed. What I tried (found from various google lookups...) is this: con =...
1
by: Sunny | last post by:
Hi, Can someone tell me how to get the content of entire xml file in Firefox? I am using this code to load the xml file. if (document.implementation && document.implementation.createDocument) {...
1
by: tom thomas | last post by:
Dear sir, i am very new to c# and i request you to... please look into the problem The following is the code(please see the code below:) which i uses to open the binary file,currently it works...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.