Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #98764
| From | kent nyberg <kent@z-sverige.nu> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Problems using struct pack/unpack in files, and reading them. |
| Date | 2015-11-13 15:15 -0500 |
| Message-ID | <mailman.303.1447445714.16136.python-list@python.org> (permalink) |
| References | <20151113192045.GA9913@z-sverige.nu> <CALwzidnL1N8efgJfM-EKTgqzmDjg2QwcZVVmDXJB2_QGyFnExA@mail.gmail.com> |
On Fri, Nov 13, 2015 at 12:36:22PM -0700, Ian Kelly wrote:
> On Fri, Nov 13, 2015 at 12:20 PM, kent nyberg <kent@z-sverige.nu> wrote:
> > def LoadCommandAndReact(place_to_read):
> > global RegisterAX
> >
> > tmp = place_to_read.read()[RegisterAX:calcsize('HH')]
>
> It looks like you're trying to get a slice of length 4 here, starting
> at the value of RegisterAX. What you're actually getting is a slice
> starting at the value of RegisterAX and ending at 4. You probably want
> this instead:
>
> tmp = place_to_read.read()[RegisterAX:RegisterAX + calcsize('HH')]
> --
> https://mail.python.org/mailman/listinfo/python-list
Even with that, it still gets wrong.
I also tried .read()[RegisterAX:RegisterAX+4]
As for the other suggestions you had in the second reply, I can comment on them later.
For now though, I just want to be able to loop this specific function.
I do appriciate that you took the time. :)
What bothers me, is the error that says
unpack requires a string argument of 4 bytes.
Im thinking in the line of arguments? Does unpack look at the 4 bytes it has read, and tell for some
reason say that unpacking needs an argument of 4 bytes? I know that I can set the arguments
for unpack. I have done that. It is: unpack('>HH', tmp).
So either '>HH' or tmp is wrong. Since '>HH' worked for the first run of the function, I assume its correct.
And as far as I know, doing .read()[RegisterAX:RegisterAX:+4] should read the following
4 bytes.
I have even tried doing .read(4), since then python should manage where to start and read 4?
Still it complains?
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Problems using struct pack/unpack in files, and reading them. kent nyberg <kent@z-sverige.nu> - 2015-11-13 15:15 -0500
csiph-web