Re: how to reading binary data...

Discussion in 'Python' started by Miki Tebeka, Oct 21, 2004.

  1. Miki Tebeka

    Miki Tebeka Guest

    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 <>
    http://tebeka.spymac.net
    The only difference between children and adults is the price of the toys
    Miki Tebeka, Oct 21, 2004
    #1
    1. Advertising

  2. On Thu, 21 Oct 2004 10:07:32 +0200, Miki Tebeka <> 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
    Bengt Richter, Oct 21, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Denise Smith
    Replies:
    2
    Views:
    642
    Denise Smith
    Nov 22, 2003
  2. My Name
    Replies:
    9
    Views:
    10,315
    Roedy Green
    Jul 21, 2004
  3. Brad Marts

    Reading binary data from file

    Brad Marts, Dec 8, 2003, in forum: C++
    Replies:
    1
    Views:
    430
    Victor Bazarov
    Dec 8, 2003
  4. Dimitri Papoutsis

    Problems with reading binary data files

    Dimitri Papoutsis, Mar 10, 2005, in forum: C++
    Replies:
    4
    Views:
    383
    Old Wolf
    Mar 11, 2005
  5. Replies:
    1
    Views:
    314
    Peter Hansen
    Oct 21, 2004
Loading...

Share This Page