开发者

Python struct.pack() not working in script

开发者 https://www.devze.com 2023-03-01 05:30 出处:网络
I have some problems here when I try to use struct.pack from a script. Everything is fine in the interpreter:

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,)
0

精彩评论

暂无评论...
验证码 换一张
取 消