Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28035

Re: issue with struct.unpack

Newsgroups comp.lang.python
Date 2012-08-28 16:55 -0700
References <2cc8b390-0b7a-4a0f-ac88-113daf65eba5@googlegroups.com> <5653b3e0-1e37-4f70-8257-6630eacdce5f@googlegroups.com> <mailman.3907.1346194193.4697.python-list@python.org> <f6154ea9-390f-44db-a485-ec85e089107f@googlegroups.com> <mailman.3911.1346196977.4697.python-list@python.org>
Subject Re: issue with struct.unpack
From 9bizy <a.m.akingbulu-11@student.lboro.ac.uk>
Message-ID <mailman.3912.1346198116.4697.python-list@python.org> (permalink)

Show all headers | View raw


On Wednesday, 29 August 2012 00:36:40 UTC+1, MRAB  wrote:
> On 29/08/2012 00:01, 9bizy wrote:> On Tuesday, 28 August 2012 23:49:54 
> 
> UTC+1, MRAB  wrote:
> 
>  >> On 28/08/2012 23:34, 9bizy wrote:
> 
>  >> > This is what I have to reproduce the challenge I am having below:
> 
>  >> >
> 
>  >> > import csv
> 
>  >> > import struct
> 
>  >> >
> 
>  >> > data = []
> 
>  >> >
> 
>  >> > for Node in csv.reader(file('s_data.xls')):
> 
>  >>
> 
>  >> That tries to read the file as CSV, but, judging from the extension,
> 
>  >> it's in Excel's format. You don't even use what is read, i.e. Node.
> 
>  >>
> 
>  >> >      data.append(list((file('s_data.xls'))))
> 
>  >> >
> 
>  >> That opens the file again and 'list' causes it to read the file as
> 
>  >> though it were a series of lines in a text file, which, as I've said,
> 
>  >> it looks like it isn't. The list of 'lines' is appended to the list
> 
>  >> 'data', so that's a list of lists.
> 
>  >> >
> 
>  >> >      data = struct.unpack('!B4HH', data)
> 
>  >> >      print "s_data.csv: ", data
> 
>  >> >
> 
>  >> > I tries so many format for the struct.unpack but I got this errors:
> 
>  >> >
> 
>  >> > Traceback (most recent call last):
> 
>  >> >
> 
>  >> >      data = struct.unpack('!B4HH', data)
> 
>  >> > struct.error: unpack requires a string argument of length 11
> 
>  >> >
> 
>  >> [snip]
> 
>  >> It's complaining because it's expecting a string argument but you're
> 
>  >> giving it a list instead.
> 
>  >
> 
>  > How do I then convert data to a string argument in this case?
> 
>  >
> 
> The question is: what are you trying to do?
> 
> 
> 
> If you're trying to read an Excel file, then you should be trying the
> 
> 'xlrd' module. You can find it here: http://www.python-excel.org/
> 
> 
> 
> If your trying to 'decode' a binary file, then you should open it in
> 
> binary mode (with "rb"), read (some of) it as a byte string and then
> 
> pass it to struct.unpack.

Thank you MRAB this was helpful.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

issue with struct.unpack 9bizy <a.m.akingbulu-11@student.lboro.ac.uk> - 2012-08-25 11:34 -0700
  Re: issue with struct.unpack Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-25 20:12 +0100
  Re: issue with struct.unpack MRAB <python@mrabarnett.plus.com> - 2012-08-25 20:16 +0100
    Re: issue with struct.unpack 9bizy <a.m.akingbulu-11@student.lboro.ac.uk> - 2012-08-28 15:35 -0700
      Re: issue with struct.unpack MRAB <python@mrabarnett.plus.com> - 2012-08-28 23:53 +0100
      Re: issue with struct.unpack Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-28 22:40 -0400
    Re: issue with struct.unpack 9bizy <a.m.akingbulu-11@student.lboro.ac.uk> - 2012-08-28 15:35 -0700
  Re: issue with struct.unpack Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-25 19:31 -0400
    Re: issue with struct.unpack Alexander Blinne <news@blinne.net> - 2012-08-26 14:53 +0200
  Re: issue with struct.unpack MRAB <python@mrabarnett.plus.com> - 2012-08-28 23:49 +0100
    Re: issue with struct.unpack 9bizy <a.m.akingbulu-11@student.lboro.ac.uk> - 2012-08-28 16:01 -0700
    Re: issue with struct.unpack MRAB <python@mrabarnett.plus.com> - 2012-08-29 00:36 +0100
      Re: issue with struct.unpack 9bizy <a.m.akingbulu-11@student.lboro.ac.uk> - 2012-08-28 16:55 -0700
      Re: issue with struct.unpack 9bizy <a.m.akingbulu-11@student.lboro.ac.uk> - 2012-08-28 16:55 -0700
  Re: issue with struct.unpack Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-28 22:31 -0400

csiph-web