473,387 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Conversion of perl unpack code to python - something odd

Hey everyone,
Maybe you can see something I don't.

I need to convert a working piece of perl code to python.

The perl code is:

sub ParseTrig {
# unpack the ibm struct that triggers messages.
my $struct = shift;

my %data;
@data{qw/StructID Version QName ProcessName TriggerData ApplType
ApplId EnvData UserData QMgrName/} =
unpack("A4A4A48A48A64A4A256A128A128A48", $struct);

return undef unless $data{StructID} eq 'TMC';

return \%data;
Taking away the fact that it is a function, the base code sets a 732
element structure with the ascii pattern derived above.

I wrote a simple test script that accepts the command argument at
position 1.

#!C:\Python24\python
import sys, string, struct

# From the language declarations in manual, the following format was derived

format='4s 4s 48s 48s 64s 4s 256s 128s 128s 48s'
size=struct.calcsize(format)

# Extract list item out for convenience/readability
data=sys.argv[1]

# Calculated size of the struct format is 732
# Length of data string is 732

print len(data)
print size

d1,d2=struct.unpack(format,data)
print d1
sys.exit(0)
When I run it, it says there are too many values to unpack.

Traceback ( most recent call last)
File "m:\mq\mq\scripts\format.py", line 32, in ?
d1, d2 = struct.unpack(format,data)
ValueError: too many values to unpack
Error starting triggered application


I checked the manual on the structure used to pack the code and it
appears correct.

#
# MQTMC2 Language declarations as defined chapt 22 of programmers ref
# http://publibfp.boulder.ibm.com/epubs/pdf/csqzak09.pdf
#
# CHAR4 Struct ID
# CHAR4 Version
# CHAR48 QName
# CHAR48 ProcessName
# CHAR64 TriggerData
# CHAR4 ApplType
# CHAR256 ApplID
# CHAR128 EnvData
# CHAR128 UserData
# CHAR48 QMgrName
Any help you can provide on this would be greatly appreciated.

May 18 '06 #1
3 3804
Andrew Robert wrote:
# From the language declarations in manual, the following format was derived

format='4s 4s 48s 48s 64s 4s 256s 128s 128s 48s'
that's 10 specifiers.
d1,d2=struct.unpack(format,data)
that's two variables.
print d1
sys.exit(0)

When I run it, it says there are too many values to unpack.

Traceback ( most recent call last)
File "m:\mq\mq\scripts\format.py", line 32, in ?
d1, d2 = struct.unpack(format,data)
ValueError: too many values to unpack
Error starting triggered application

I checked the manual on the structure used to pack the code and it
appears correct.


try printing the return value from struct.unpack, and see if you can
figure out what's wrong with your code:

print struct.unpack(format,data)

</F>

May 18 '06 #2
Andrew Robert wrote:
format='4s 4s 48s 48s 64s 4s 256s 128s 128s 48s'
You are trying to squeeze 10 items into just
d1,d2=struct.unpack(format,data)
two variables (d1 and d2)
ValueError: too many values to unpack


and Python is quite explicit that it doesn't like that once you realize that
'unpack' doesn't refer to struct.unpack() but to tuple unpacking like
a, b = "ab"
a, b = "abc"

Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: too many values to unpack
Peter

May 18 '06 #3
Peter Otten wrote:
Andrew Robert wrote:
format='4s 4s 48s 48s 64s 4s 256s 128s 128s 48s'


You are trying to squeeze 10 items into just
d1,d2=struct.unpack(format,data)


two variables (d1 and d2)
ValueError: too many values to unpack


and Python is quite explicit that it doesn't like that once you realize that
'unpack' doesn't refer to struct.unpack() but to tuple unpacking like
a, b = "ab"
a, b = "abc"

Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: too many values to unpack
Peter

Now I feel like a first class idiot.

Thanks for the help.

Added the extra eight variables and things worked perfectly.

Going to go stand in the corner for an hour now :)

May 18 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Idar | last post by:
Is there an effecient/fast way in python to convert binary data from file (24bit hex(int) big endian) to 32bit int (little endian)? Have seen struct.unpack, but I am unsure how and what Python has...
5
by: Geoffrey | last post by:
Hope someone can help. I am trying to read data from a file binary file and then unpack the data into python variables. Some of the data is store like this; xbuffer:...
5
by: Koncept | last post by:
Howdie Python folks! I am very new to Python ( 3rd day now ) and it has already earned its place as my fav. language to work in. I hope to continue, and I really would appreciate some good...
0
by: magoo | last post by:
Gentlemen, Recently I had need to roll my own conversion routine for reading data values from a SEGY file (seismic data values) which were in IBM 32-bit floating point format. I would like to...
4
by: Stacy Mader | last post by:
Greetings all, I have a VMS binary file with weather data. The record on each line is 1xint(8) 12xint(2) Using the perl unpack function, I can decode the binary file like this: <snip>
32
by: Bob Greschke | last post by:
I'm reading 3-byte numbers from a file and they are signed (+8 to -8million). This seems to work, but I'm not sure it's right. # Convert the 3-characters into a number. Value1, Value2, Value3 =...
0
by: Gary Herron | last post by:
Marlin Rowley wrote: You don't need the newsgroup to answer this kind of question. Just try it! abcdefghi abcd Notice that the index does not change the original array. Gary Herron
0
by: Gary Herron | last post by:
Marlin Rowley wrote: Numpy can do this for you. First, do you really mean the array to contain lists of one string each? If so: kludge here array(, dtype='|S1') array(, ,
1
by: DJJohnnyG | last post by:
Hi, I am trying to recreate a perl script in C# but have a problem creating a checksum value that a target system needs. In Perl this checksum is calculated using the unpack function: while...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.