【发布时间】:2011-05-08 03:09:35
【问题描述】:
试图从二进制数的左端去除“0b1”。
以下代码导致剥离所有二进制对象。 (不好)
>>> bbn = '0b1000101110100010111010001' #converted bin(2**24+**2^24/11)
>>> aan=bbn.lstrip("0b1") #Try stripping all left-end junk at once.
>>> print aan #oops all gone.
''
所以我分两步完成了 .lstrip():
>>> bbn = '0b1000101110100010111010001' # Same fraction expqansion
>>> aan=bbn.lstrip("0b")# Had done this before.
>>> print aan #Extra "1" still there.
'1000101110100010111010001'
>>> aan=aan.lstrip("1")# If at first you don't succeed...
>>> print aan #YES!
'000101110100010111010001'
怎么了?
再次感谢您通过一个简单的步骤解决此问题。 (见我之前的问题)
【问题讨论】:
-
Reading documentation helps ;) : chars 参数不是前缀;相反,它的值的所有组合都会被剥离。
-
类似于为什么 str.lstrip 会去除多余的字符? stackoverflow.com/questions/1687171/…