Reading binary from a file...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • KraftDiner

    #1

    Reading binary from a file...

    Hi!
    In python I'm able to read in binary data from a file.
    data = file.read() # Reads in an entire file.

    However the data is 16bits per sample and python is storing the
    data in a string. How do I convert that 8bit data into a list of 16
    bit integers?

    Note: I want generic python lists or tupels not numpy or numeric etc
    etc...

  • Steve Holden

    #2
    Re: Reading binary from a file...

    KraftDiner wrote:[color=blue]
    > Hi!
    > In python I'm able to read in binary data from a file.
    > data = file.read() # Reads in an entire file.
    >[/color]
    Note that you should open the file in binary mode to be
    platform-agnostic and as portable as possible. (Just in case you aren't).
    [color=blue]
    > However the data is 16bits per sample and python is storing the
    > data in a string. How do I convert that 8bit data into a list of 16
    > bit integers?
    >
    > Note: I want generic python lists or tupels not numpy or numeric etc
    > etc...
    >[/color]
    You really want the struct module here.

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd www.holdenweb.com
    Love me, love my blog holdenweb.blogs pot.com

    Comment

    • Laurent Pointal

      #3
      Re: Reading binary from a file...

      Steve Holden a écrit :[color=blue]
      > KraftDiner wrote:[color=green]
      >> Hi!
      >> In python I'm able to read in binary data from a file.
      >> data = file.read() # Reads in an entire file.
      >>[/color]
      > Note that you should open the file in binary mode to be
      > platform-agnostic and as portable as possible. (Just in case you aren't).
      >[color=green]
      >> However the data is 16bits per sample and python is storing the
      >> data in a string. How do I convert that 8bit data into a list of 16
      >> bit integers?
      >>
      >> Note: I want generic python lists or tupels not numpy or numeric etc
      >> etc...
      >>[/color]
      > You really want the struct module here.[/color]

      If all data are same 16 bits integer values, you can also see the array
      module (and the fromfile() method - see docs).

      A+

      Laurent.

      Comment

      • Bryan Olson

        #4
        Re: Reading binary from a file...

        KraftDiner wrote:
        [...][color=blue]
        > In python I'm able to read in binary data from a file.[/color]
        [...][color=blue]
        >
        > However the data is 16bits per sample and python is storing the
        > data in a string. How do I convert that 8bit data into a list of 16
        > bit integers?[/color]

        On the vast majority of systems, files hold sequences of
        eight-bit integers. How you convert from those to your 16-bit
        type depends on how the the writer of the file converted the
        16-bit integer type to a sequence 8-bit integers.


        --
        --Bryan

        Comment

        Working...