我有一些简单的python代码搜索文件中的字符串,例如path=c:\path,c:\path可能会有所不同.目前的代码是:
def find_path(i_file):
lines = open(i_file).readlines()
for line in lines:
if line.startswith("Path="):
return # what to do here in order to get line content after "Path=" ?
Run Code Online (Sandbox Code Playgroud)
之后获取字符串文本的简单方法是什么Path=?有没有简单的方法,没有封闭,反射或其他深奥的东西?
试图从二进制数的左端去掉"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.
''
Run Code Online (Sandbox Code Playgroud)
所以我分两步完成了.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'
Run Code Online (Sandbox Code Playgroud)
这是怎么回事?
再次感谢您通过一个简单的步骤解决此问题.(见我之前的问题)
我试图从字符串"@@@@ b @@"中删除所有前缀"@".预期输出为"b @@"(不是所有'@'但只有前缀)如果没有前缀"@" ,它应该返回原始字符串本身这是代码,我正在尝试:(我正在使用python 2.X)
mylist = []
def remove(S):
mylist.append(S)
j=0
for i in range(len(S)):
if mylist[0][j]=='@':
S = S[:j] + S[j + 1:]
j+=1
return S
else:
return S
break
a = remove("@@@@b@@")
print a
Run Code Online (Sandbox Code Playgroud)