Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old June 25th, 2003, 11:11 AM
shochu shochu is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 6 shochu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Reading Binary

Hi I'm totally new to python and have had no experience reading binary files so I might be totally off here.

I have a file which consists of 16 bit unsigned short ints, one right after the other. The file is in binary. I want to read it in and print the ints but not as binary as actual ints.

I tried just reading the file in two bytes at a time using read() and then printing the data but that just printed unreadable stuff.

Something like this...

file = open(filename, 'rb')
data = int(file.read(2))
print data #doesn't work of course

Anyone know how I could do this or if I can do it at all?? Thanks for the help!

Reply With Quote
  #2  
Old June 25th, 2003, 02:59 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Binary a'

Well I'm impressed, for a total newbie to Python you worked out how to read in a binary file correctly the first time. here's a quick example anyway..

Code:
file = open('filename', 'rb').read()


I think the problem your having is that binary files are generally compiled or "not in plain english" and contain a whole bunch of strange and wonderful charactars. (please correct me if i'm wrong).

Could you possibly attach the file your trying to read so I can see what your working with, you might also want to try opening the file in your favourate text editor and seeing if the content is readable or not.

Have fun,
Mark.

Reply With Quote
  #3  
Old July 1st, 2003, 10:05 AM
shochu shochu is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 6 shochu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
To read binary you can use the struct module or the array module.

If you want to use the array module it needs to be one consistent type (ie. all chars, all ints, all floats)

You can stream the entire binary file into the array.

For instance, in my case I used 16 bit integers so the code is as follows

from array import *

data = array('h') #h is the type, fill in whatever type you need
data.fromfile(file, numBytes) #numBytes is how many bytes you want to read in
data.byteswap() #nifty little function for going from little to big endian or vice versa

That's it! You can then access each piece of data jsut like normal

print data[0] #etc. etc.

Last edited by shochu : July 1st, 2003 at 10:08 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Reading Binary


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway