1

I have some problems here when I try to use struct.pack from a script. Everything is fine in the interpreter:

>>> import struct  
>>> k=2  
>>> struct.pack(">b", k)  
'\x02'  

Now when I do the same from a script I have problems:

k=2  
p =  struct.pack(">b", k)  
print "p is %s"%(p,)  
return p

Result:

p is 

what am I doing wrong? I really don't understand this and would be glad if somebody could help me. Thanks

0

3 Answers 3

8

Everything is fine. The character is unprintable.

print "p is %r" % (p,)
Sign up to request clarification or add additional context in comments.

Comments

0

In the interpreter it is displaying the repr of that char and it is interpreting it when you do a print. So you can just do repr(p) in your script if you want to have same result as the interpreter.

Comments

0

You are actually printing character '\x02' which is not visible. Try printing it's representation instead.

print "p is %r"%(p,)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.