Reading binary from a file...

K

KraftDiner

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...
 
S

Steve Holden

KraftDiner said:
Hi!
In python I'm able to read in binary data from a file.
data = file.read() # Reads in an entire file.
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).
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...
You really want the struct module here.

regards
Steve
 
L

Laurent Pointal

Steve Holden a écrit :
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).

You really want the struct module here.

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

A+

Laurent.
 
B

Bryan Olson

KraftDiner wrote:
[...]
In python I'm able to read in binary data from a 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?

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top