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
Everything is fine. The character is unprintable.
print "p is %r" % (p,)
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.
You are actually printing character '\x02
' which is not visible. Try printing it's representation instead.
print "p is %r"%(p,)
精彩评论