如果我正確理解你的問題,你想將result
設置爲ggggg
行之前的所有內容?
你可以嘗試以下方法:
result = ''
with open('text','r') as f: // Open file 'text' as 'r'eadonly,
f.seek(0) // move the readcursor to the beginning of the document
for line in f: // for each line...
if not line.startswith('ggggg'): // If 'ggggg' isn't at the beginning of the line..
result = "{0}\n{1}".format(result, line) // append the line to the result variable.
else:
break
f.close()
如果你寧願讓這個它只是忽略了ggggg
線,得到了一切,然後嘗試:
result = ''
with open('text','r') as f: // Open file 'text' as 'r'eadonly,
f.seek(0) // move the readcursor to the beginning of the document
for line in f: // for each line...
if not line.startswith('ggggg'): // If 'ggggg' isn't at the beginning of the line..
result = "{0}\n{1}".format(result, line) // append the line to the result variable.
else:
continue
f.close()
'str.split()'不需要正則表達式。 –