Hi;
I'm trying to use the struct.unpack to extract an int, int, char
struct info from a file. I'm more accustomed to the file.readlines
which works well in a 'for' construct (ending loop after reaching
EOF).
# This does OK at fetching one 10-byte string at a time:
# (4, 4, 2 ascii chars representing hex)
info1, info2, info3 = struct.unpack('<IIH', myfile.read(10))
# Now to do the entire file, putting into a loop just gives error:
# TypeError: 'int' object is not iterable
for info1, info2, info3 in struct.unpack('<IIH', myfile.read(10)):
In trying to shoehorn this into a 'for' loop I've been unsuccessful.
I also tried other variations that also didn't work but no point
wasting space. Using Python 2.5, WinXP
Thx,
Mark 6 5138
On Mon, Oct 27, 2008 at 4:29 PM, Mark <ms*****@gmail.comwrote:
Hi;
I'm trying to use the struct.unpack to extract an int, int, char
struct info from a file. I'm more accustomed to the file.readlines
which works well in a 'for' construct (ending loop after reaching
EOF).
# This does OK at fetching one 10-byte string at a time:
# (4, 4, 2 ascii chars representing hex)
info1, info2, info3 = struct.unpack('<IIH', myfile.read(10))
# Now to do the entire file, putting into a loop just gives error:
# TypeError: 'int' object is not iterable
for info1, info2, info3 in struct.unpack('<IIH', myfile.read(10)):
In trying to shoehorn this into a 'for' loop I've been unsuccessful.
I also tried other variations that also didn't work but no point
wasting space. Using Python 2.5, WinXP
Thx,
Mark
-- http://mail.python.org/mailman/listinfo/python-list
I usually do something like:
s = myfile.read(10)
while len(s) == 10:
info1, info2, info3 = struct.unpack('<IIH', s)
s = myfile.read(10)
#might want to check that len(s) == 0 here
En Mon, 27 Oct 2008 19:03:37 -0200, Steven Clark
<st************@gmail.comescribió:
On Mon, Oct 27, 2008 at 4:29 PM, Mark <ms*****@gmail.comwrote:
>Hi;
I'm trying to use the struct.unpack to extract an int, int, char struct info from a file. I'm more accustomed to the file.readlines which works well in a 'for' construct (ending loop after reaching EOF).
# This does OK at fetching one 10-byte string at a time: # (4, 4, 2 ascii chars representing hex) info1, info2, info3 = struct.unpack('<IIH', myfile.read(10))
# Now to do the entire file, putting into a loop just gives error: # TypeError: 'int' object is not iterable for info1, info2, info3 in struct.unpack('<IIH', myfile.read(10)):
In trying to shoehorn this into a 'for' loop I've been unsuccessful. I also tried other variations that also didn't work but no point wasting space. Using Python 2.5, WinXP
I usually do something like:
s = myfile.read(10)
while len(s) == 10:
info1, info2, info3 = struct.unpack('<IIH', s)
s = myfile.read(10)
#might want to check that len(s) == 0 here
Pretty clear. Another alternative, using a for statement as the OP
requested (and separating the "read" logic from the "process" part):
def chunked(f, size):
while True:
block = f.read(size)
if not block: break
yield block
for block in chunked(open(filename, 'rb'), 10):
info1, info2, info3 = struct.unpack('<IIH', block)
...
A third one:
from functools import partial
for block in iter(partial(open(filename,'rb').read, 10), ''):
...
(rather unreadable, I admit, if one isn't familiar with partial functions
and the 2-argument iter variant)
--
Gabriel Genellina
Mark wrote:
Hi;
I'm trying to use the struct.unpack to extract an int, int, char
struct info from a file. I'm more accustomed to the file.readlines
which works well in a 'for' construct (ending loop after reaching
EOF).
You do not need .readlines to iterate through a file by lines.
for line in f.readlines():pass
is awkward if you have 100million 100 byte lines, whereas
for line in f: pass
will read one line at a time and process before reading the next.
>
# This does OK at fetching one 10-byte string at a time:
# (4, 4, 2 ascii chars representing hex)
info1, info2, info3 = struct.unpack('<IIH', myfile.read(10))
# Now to do the entire file, putting into a loop just gives error:
# TypeError: 'int' object is not iterable
for info1, info2, info3 in struct.unpack('<IIH', myfile.read(10)):
In trying to shoehorn this into a 'for' loop I've been unsuccessful.
for loops require an iterator. Files only come with one. So either use
a while loop or define a reusable file-block generator (untested):
def blocks(openfile, n):
while True:
block = openfile.read(n)
if len(block) == n:
yield block
else:
raise StopIteration
Terry Jan Reedy
Thanks I tested your solution and that works.
One of the things that didn't work was
for chunk in myfile.read(10):
info1, info2, info3 = struct.unpack('<IIH', chunk)
It gets an error saying unpack requires a string of length 10, which I
thought chunk would be after the read(10). I'm still a little
confused as to why.
But thanks very much Steven, for a working solution.
Mark
Mark wrote:
Thanks I tested your solution and that works.
One of the things that didn't work was
for chunk in myfile.read(10):
info1, info2, info3 = struct.unpack('<IIH', chunk)
It gets an error saying unpack requires a string of length 10, which I
thought chunk would be after the read(10). I'm still a little
confused as to why.
this code python interprets as:
data = myfile.read(10)
for chunk in data:
<and here chunk is a single-character string>.
--Scott David Daniels Sc***********@Acm.Org
this code python interprets as:
data = myfile.read(10)
for chunk in data:
<and here chunk is a single-character string>.
Aha - now that you put it that way it makes sense. And thanks to all
who replied - I'll try out the other suggestions too.
Mark This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: David Buchan |
last post by:
Hi guys,
This may be a dumb question; I'm just getting into C language here.
I wrote a program to unpack a binary file and write out the contents to
a new file as a list of unsigned integers....
|
by: Marius Cabas |
last post by:
Hi, I'm a beginner so don't shoot ;)
I'm reading a wave file into a byte and I'm trying to convert the result
to String but the converted string is altered, so if I'm generating a new
wave file...
|
by: Tomás |
last post by:
When you have compile-time strings like:
"The file is of unknown format."
What kind of variables do you use to store it? At the moment I'm using:
char const str = "The file is of unknown...
|
by: Jim Michaels |
last post by:
after a file upload, $_FILES is not populated but $_POST is. what's going
on here?
$_POST=C
$_POST=C
$_POST=C
$_POST=C:\\www\\jimm\\images\\bg1.jpg
$_FILES=
$_FILES=
$_FILES=
|
by: ram |
last post by:
Stupid question #983098403:
I can't seem to pass an unpacked sequence and keyword arguments to a
function at the same time. What am I doing wrong?
def f(*args, **kw):
for a in args:
print...
| |
by: Chris Garland |
last post by:
What's wrong here?
I can unpack an unsigned char
(144,)
I can unpack a short
(6,)
But an unsigned char & a short give me this
|
by: p.lavarre |
last post by:
How do I vary the byte offset of a field of a ctypes.Structure?
How do I "use the dynamic nature of Python, and (re-)define the data
type after the required size is already known, on a case by...
|
by: dgleeson3 |
last post by:
Hello All
I have a txt file of strings of different lengths.
I dont know how many strings are in the file.
I have no problem reading the file and sending to the console (as
below).
To...
|
by: =?Utf-8?B?SXpvcmljaA==?= |
last post by:
I observed that WCF client running inside Full Trust mode XBAP application
can't read byte array over 16384. If return result is bigger than that size,
then client simply get null or Nothing in VB...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |