我試圖創建一個程序,它將刪除*或!如果他們以所述字符開始,則從行。因此,像:在Python 3.5中刪除txt中的特定字符
*81
!81
將改變爲:
81
81
這是我使用的是截至目前代碼:
input("Hello")
with open("Test.txt",'r') as c:
lines = c.readlines()
c.close()
with open("Test.txt",'w') as c:
c.truncate()
for line in lines:
if line.startswith("!") or line.startswith("*") == False:
c.write(line)
if line.startswith("!") or line.startswith("*") == True:
new_line = line.translate({ord(c): None for c in '* !'})
print(new_line)
c.write(new_line)
c.close()
然而,只有明星會刪除,這是什麼問題?