的。如果你只有以==
或foo=bar
開頭的行纔可以使用正常的str.replace。
s="""
head1 | head2 | head3
=======================
foo=bar | baz | quuux
"""
out = ""
for line in s.splitlines(True):
if line.startswith("="): # or if line[0]== "="
out += line.replace("=", "-")
else:
out += line
print(out)
head1 | head2 | head3
-----------------------
foo=bar | baz | quuux
這應該覆蓋情況下,你只需要在該行=比正則表達式更高效:
for line in s.splitlines(True):
if not line.rstrip().translate(None,"="): # or if line[0]== "="
out += line.replace("=","-")
else:
out += line
print(out)
什麼是你期望的輸出?你正在運行哪種語言? – 2015-01-21 11:30:17