struct unpack to pre-allocated array?

2009-01-09 Thread Rich Henry
Is there any way to struct.unpack or struct.unpack_from into an existing array.array or similar structure? I am unpacking file data in a loop and i was hoping to find something that performs better than simply unpacking into a new tuple each iteration. Thanks in advance, Rich -- http://mail.python

Re: struct unpack issue

2008-06-13 Thread Miles
On Fri, Jun 13, 2008 at 9:27 PM, Ping Zhao <[EMAIL PROTECTED]> wrote: > However, when I tried to rewrite them in short: > > header = struct.unpack('2s1i', self.__read(src, 6)) > ... > It was weired that the required argument length increased to 8. This is an alignment issue; if you don't specify a

Re: struct unpack issue

2008-06-13 Thread Peter Otten
Ping Zhao wrote: > I am writing a small program to decode MS bitmap image. When I use > statements as follow, it works fine: > > header['sig'] = str(struct.unpack('2s', self.__read(src, 2))[0]) > header['len'] = int(struct.unpack('1i', self.__read(src, 4))[0]) > > However, when I tried to rewr

struct unpack issue

2008-06-13 Thread Ping Zhao
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Hi, I am writing a small program to decode MS bitmap image. When I use statements as follow, it works fine: header['sig'] = str(struct.unpack('2s', self.__read(src, 2))[0]) header['len'] = int(struct.unpack('1i', self.__read(src, 4))[0]) However,

Re: struct unpack

2008-03-17 Thread Mark Tolonen
"brnstrmrs" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > If I run: > > testValue = '\x02\x00' > junk = struct.unpack('h', testValue) > > Everything works but If I run > > testValue = raw_input("Enter Binary Code..:") inputting at the > console '\x02\x00' > junk = struct.unpack('

Re: struct unpack

2008-03-17 Thread Ivan Illarionov
On Mar 17, 11:00 pm, brnstrmrs <[EMAIL PROTECTED]> wrote: > If I run: > > testValue = '\x02\x00' > junk = struct.unpack('h', testValue) > > Everything works but If I run > > testValue = raw_input("Enter Binary Code..:") inputting at the > console '\x02\x00' > junk = struct.unpack('h', testValue) >

Re: struct unpack

2008-03-17 Thread Michael Wieher
> > testValue = '\x02\x00' > junk = struct.unpack('h', testValue) #Works > > testValue = raw_input("Enter Binary Code..:") inputting at the > console '\x02\x00' > junk = struct.unpack('h', testValue) > > error: unpack requires a string argument of length 2 Well, it thinks the length of the test

struct unpack

2008-03-17 Thread brnstrmrs
If I run: testValue = '\x02\x00' junk = struct.unpack('h', testValue) Everything works but If I run testValue = raw_input("Enter Binary Code..:") inputting at the console '\x02\x00' junk = struct.unpack('h', testValue) It errors out with Traceback (most recent call last): File "/home/nirmal/

Re: struct unpack newline

2005-05-31 Thread grant
Good point. Hadn't thouhgt of that. Thanks Grant -- http://mail.python.org/mailman/listinfo/python-list

Re: struct unpack newline

2005-05-31 Thread Peter Otten
[EMAIL PROTECTED] wrote: > what concerns me though is that although the file is opened in binary > mode, > "for line.." has a problem reading the file correctly. There is _no_ correct way of splitting a file containing binary data in lines because binary data may contain newline bytes that do not

Re: struct unpack newline

2005-05-30 Thread grant
Hi , Thanks for the tip regarding checking the length of line. I discovered that on the problem record it was short by a few bytes. After changing the read method from "for line in.." to "infile.read(n)" my problem was solved, what concerns me though is that although the file is opened in binary m

Re: struct unpack newline

2005-05-27 Thread Richard Brodie
<[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > except when line[86:90] contains "carriage-return" "linefeed" > which are valid binary packed values. You probably don't want to be reading binary data a line at a time, if that's what you're doing. -- http://mail.python.org/mailma

Re: struct unpack newline

2005-05-27 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote: > I am pretty new to python and am having a problem > intepreting binary data using struct.unpack. > I am reading a file containing binary packed data > using open with "rb". All the values are coming through > fine when using (integer1,) = struct.unpack('l', line[86:90])

struct unpack newline

2005-05-27 Thread grant
Hi All, I am pretty new to python and am having a problem intepreting binary data using struct.unpack. I am reading a file containing binary packed data using open with "rb". All the values are coming through fine when using (integer1,) = struct.unpack('l', line[86:90]) except when line[86:90] con