-4
A
回答
1
with open(your_f) as f:
lines = f.readlines()
for ind, line in enumerate(lines):
if your condition: # if line contains a match
lines[ind] ="" # set line to empty string
with open(your_f,"w") as f: # reopen with w to overwrite
f.writelines(lines) # write updated lines
例如除去一個txt文件中的一行,與55開始:
with open("in.txt") as f:
lines = f.readlines()
for ind, line in enumerate(lines):
if line.startswith("55"):
lines[ind] = ""
with open("in.txt","w") as f:
f.writelines(lines)
輸入:
foo
bar
55 foobar
44 foo
輸出:
foo
bar
44 foo
+0
非常感謝你! – 2014-11-06 19:19:27
相關問題
- 1. 刪除文件和記錄
- 2. 從文件中刪除記錄?
- 3. 如何從文件中刪除記錄?
- 4. 單擊按鈕時記錄刪除的文件?記錄所有文件(刪除和未刪除)
- 5. 使用VBScript記錄刪除的文件
- 6. 刪除子記錄刪除實體中的父記錄
- 7. 將所有未記錄的文件刪除到Git目錄中?
- 8. 從FASTA文件刪除第一條記錄在Python
- 9. 刪除記錄
- 10. 刪除記錄
- 11. 刪除Python Selenium驗證記錄
- 12. 從excel中刪除記錄
- 13. 從Git歷史記錄中刪除具有歷史記錄的特定文件
- 14. 如何取消刪除以前在git歷史記錄中刪除的文件?
- 15. 刪除unforgeted記錄
- 16. Openerp刪除記錄
- 17. 多刪除記錄
- 18. Cassandra刪除記錄
- 19. python-eve oplog記錄是否應該刪除文檔正文?
- 20. Python重複數據刪除記錄 - 重複數據刪除
- 21. 如何從mercurial的歷史記錄中刪除大文件?
- 22. Java刪除或修改文件中的一個記錄
- 23. 刪除二進制文件中的記錄?
- 24. 如何從我的隨機訪問文件中刪除記錄?
- 25. 在git中恢復已刪除的歷史記錄文件
- 26. 刪除與文件夾中記錄相關的圖像
- 27. 從git歷史記錄中刪除文件的某些內容
- 28. 上傳文件時從數據庫中刪除的記錄
- 29. 在TFS中刪除包含歷史記錄的文件
- 30. 在SVN中恢復刪除文件的歷史記錄
刪除整個內容或只是找到的行?另外,你有沒有試過,你有什麼計劃來完成這個? – 2014-11-06 19:05:56
只是發現行 – 2014-11-06 19:07:48