Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: how to reading binary data...

Reply
Thread Tools

Re: how to reading binary data...

 
 
Miki Tebeka
Guest
Posts: n/a
 
      10-21-2004
Hello Sandeep,

> I opened any particular file with the 'rb' mode that is read binary
> mode. so python will treat the data as a raw data. now i want to read
> first 4 bytes only then i will convert the first 4 bytes into long
> datatype and then again read 4 bytes and will do the same.
> But how to set & move the pointer using loop?

Each time you read from a file the "file pointer" is automatically
incrmented.

>>> f = open(".bashrc")
>>> f.tell()

0L
>>> f.read(1)

'#'
>>> f.tell()

1L
See http://docs.python.org/lib/bltin-file-objects.html

> also how to convert into long?

http://docs.python.org/lib/module-struct.html

HTH.
--
------------------------------------------------------------------------
Miki Tebeka <(E-Mail Removed)>
http://tebeka.spymac.net
The only difference between children and adults is the price of the toys
 
Reply With Quote
 
 
 
 
Bengt Richter
Guest
Posts: n/a
 
      10-21-2004
On Thu, 21 Oct 2004 10:07:32 +0200, Miki Tebeka <(E-Mail Removed)> wrote:

>Hello Sandeep,
>
>> I opened any particular file with the 'rb' mode that is read binary
>> mode. so python will treat the data as a raw data. now i want to read
>> first 4 bytes only then i will convert the first 4 bytes into long
>> datatype and then again read 4 bytes and will do the same.
>> But how to set & move the pointer using loop?

>Each time you read from a file the "file pointer" is automatically
>incrmented.
>
>>>> f = open(".bashrc")
>>>> f.tell()

>0L
>>>> f.read(1)

>'#'
>>>> f.tell()

>1L
>See http://docs.python.org/lib/bltin-file-objects.html
>
>> also how to convert into long?

>http://docs.python.org/lib/module-struct.html
>
>HTH.


Beware of cooked input if you want to convert binary data, if you're not on unix.
E.g., windows: (tell seems to tell the binary truth, but read collapses \r\n to \n
unless you use 'rb' mode)

>>> open('linesofthree.txt','rb').read()

'012\r\n567\r\nABC\r\n'
>>> f = open('linesofthree.txt')
>>> while True:

... pos = f.tell()
... byt = f.read(1)
... print '[%s:%r]' % (pos, byt),
... if not byt: break
...
[0:'0'] [1:'1'] [2:'2'] [3:'\n'] [5:'5'] [6:'6'] [7:'7'] [8:'\n'] [10:'A'] [11:'B'] [12:'C'] [13
:'\n'] [15:'']
>>> f = open('linesofthree.txt','rb')
>>> while True:

... pos = f.tell()
... byt = f.read(1)
... print '[%s:%r]' % (pos, byt),
... if not byt: break
...
[0:'0'] [1:'1'] [2:'2'] [3:'\r'] [4:'\n'] [5:'5'] [6:'6'] [7:'7'] [8:'\r'] [9:'\n'] [10:'A'] [11
:'B'] [12:'C'] [13:'\r'] [14:'\n'] [15:'']
>>> print open('linesofthree.txt','rb').read()

012
567
ABC


Regards,
Bengt Richter
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
(8-bit binary to two digit bcd) or (8-bit binary to two digit seven segment) Fangs VHDL 3 10-26-2008 06:41 AM
writing binary file (ios::binary) Ron Eggler C++ 9 04-28-2008 08:20 AM
A 64-bit binary returning a value to a 32-bit binary? spammenotplui31@yahoo.ca C Programming 12 04-08-2007 07:02 AM
Re: ostreams, ios::binary, endian, mixed binary-ascii Marc Schellens C++ 8 07-15-2003 12:27 PM



Advertisments