0
>>> pkt = sniff(count=2,filter="tcp")
>>> raw  = pkt[1].sprintf('%Padding.load%')
>>> raw
"'\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x00g\\xc4|\\x00\\x00\\x00\\x00\\x00'"


>>> print raw
'\x04\x00\x00\x00\x00\x00\x00\x00g\xc4|\x00\x00\x00\x00\x00'

印刷を使用すると生の出力が異なる

4

2 に答える 2

5

1 つはrepr()文字列の表現で、もう 1 つは印刷された文字列です。インタープリターに貼り付けて、同じ文字列を再度作成できる表現。

Python 対話型プロンプトはrepr()、変数をエコーするときにprint常に使用し、常にstr()文字列表現を使用します。

それ以外は同じです。print repr(raw)比較してみてください:

>>> "'\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x00g\\xc4|\\x00\\x00\\x00\\x00\\x00'"
"'\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x00g\\xc4|\\x00\\x00\\x00\\x00\\x00'"
>>> print "'\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x00g\\xc4|\\x00\\x00\\x00\\x00\\x00'"
'\x04\x00\x00\x00\x00\x00\x00\x00g\xc4|\x00\x00\x00\x00\x00'
>>> print repr("'\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x00g\\xc4|\\x00\\x00\\x00\\x00\\x00'")
"'\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x00g\\xc4|\\x00\\x00\\x00\\x00\\x00'"
于 2012-11-21T19:06:03.463 に答える
1

__str__クラスの__repr__組み込みメソッドは、必要な文字列値を返すことができます。一部のクラスは、単に repr に str() を使用します。

class AClass(object):

   def __str__(self):
      return "aclass"

   def __repr__(self):
      return str(self)

class AClass2(AClass):

   def __repr__(self):
      return "<something else>"

In [2]: aclass = AC
AClass   AClass2  

In [2]: aclass = AClass()

In [3]: print aclass
aclass

In [4]: aclass
Out[4]: aclass

In [5]: aclass2 = AClass2()

In [6]: print aclass2
aclass

In [7]: aclass2
Out[7]: <something else>

In [8]: repr(aclass2)
Out[8]: '<something else>'

In [9]: repr(aclass)
Out[9]: 'aclass'

reprこのインスタンスの束を含むリストを印刷するときなど、クラスの「ラベル」を表示することを単に意図しています...どのように見えるべきか。

strインスタンスを操作で使用する適切な文字列値に変換する方法です。

于 2012-11-21T19:09:46.360 に答える