導航:首頁 > 編程語言 > python刪除txt文本某行

python刪除txt文本某行

發布時間:2022-07-26 08:31:07

『壹』 python 刪除多個文本里的指定行

代碼基於python 2.6。功能已寫成函數,用的簡單語法,很好懂。
新文件文件名自動附加"_back"。不懂再問。

import os, time

def readKeys(fileName):
keys = []
f = open(fileName, "r")
while True:
line = f.readline()
if not line:
break
key = line.strip()
if key: keys.append(key)
f.close()
return keys

def processKeys(editFileName, backFileName, keys):
f = open(editFileName, "r")
lines = f.readlines()
f.close()

editLines = []
backLines = []

for line in lines:
found = False
for key in keys:
if line.startswith(key):
backLines.append(line)
found = True
break
if not found:
editLines.append(line)

if backLines:
f = open(editFileName, "w")
f.writelines(editLines)
f.close()
f = open(backFileName, "w")
f.writelines(backLines)
print 'modify',editFileName,'save',backFileName

if __name__ == '__main__':
keys = readKeys("0.txt")
fileList = ["1.txt", "2.txt", "3.txt", "4.txt", "5.txt"]
while True:
for fileName in fileList:
base, ext = os.path.splitext(fileName)
processKeys(fileName, base + "_back" + ext, keys)
print 'sleep 30 seconds'
time.sleep(30)

『貳』 python 刪除特定幾行

思路:

先讀入一個文件,自動保存為列表類型,每行為一個元素

然後刪除列表中索引為 0,1,2 的三個元素


以下代碼調試通過:

f = open("/Users/mfhj-dz-001-119/Downloads/text001.txt")
lines = f.readlines()
print(' original lines is:', lines)
del lines[0:2]
print(' now the lines is:', lines)


讀入的文件:

『叄』 python遍歷文本並刪除特定內容

按 指定行數 分頁讀取好處理:

defiterpage(istream,pagesize):
buffer=[]
fordatainistream:
buffer.append(data)
iflen(buffer)>=pagesize:
yieldbuffer
buffer=[]
ifbuffer:
yieldbuffer

withopen("source.txt",'rt')ashandle:
forpageiniterpage(handle,1000):
printpage#oryourbusinesslogical
print"-"*32#pagebreak

刪除文本文件的前N行:

defremovehead(filename,headlines):
buffer=[]
withopen(filename,'rt')ashandle:
fori,lninenumerate(handle):
ifln<headlines:
continue
buffer.append(ln)

withopen(filename,'wt')ashandle:
handle.writelines(buffer)

或者:

defgetandremovehead(filename,headlines):
withopen(filename,'rt')ashandle:
buffer=handle.readlines()
withopen(filename,'wt')ashandle:
handle.writelines(buffer[headlines:])
returnbuffer[:headlines]

但遇到大文本文件時,刪除其中N行不是很理想的業務方案

『肆』 python 怎麼刪除指定的行和空行

if l[:-1].strip() l是從舊文件里讀出來的每一行,判斷如果不是空行,則把這一行存到列表中,再將新的列表按行寫入新文件。 首先strip()是去除空白字元的意思。 l[:-1].strip()是把這一行中除了最後那個換行符去掉,然後再去掉空白字元得到的字元串 如果去掉換行符和空白符後得到的是空字元串的話,這一行就被拋棄,否則加入新的列表,等待寫入。

『伍』 如何用python刪除txt中指定段落的內容 比如txt內容是 <a> x <b> <a> y <b> 我

importre
text=open(r"test.txt").read()
rep=re.escape(r"y")
text=re.sub("<a> "+rep+" <b>","<a> <b>",text)
f=open(r"test.txt","w")
f.write(text)
f.close()

『陸』 python txt文件中刪除

在對文件寫入內容時,在某個條件下要清除文件內容再進行寫入操作時,就可以使用truncate(size),不用刪除文件再從新創建。

我們可以使用 truncate(size)函數來刪除 txt 文件內容,size是表示從第幾個位元組開始清除,如果是0時代表是刪除全部內容

『柒』 python刪除txt檔案的首行的空行

f=open(你的文件),得到文件句柄。
讀整個就是s= f.read()
你想去掉頭部那些空行,用s=s.lstrip()就能把字元串左邊那些空格換行通通去掉了。

『捌』 python如何刪除含有某個字元的行

#!/usr/bin/python
# -*- coding:utf-8 -*-
import re
import os
file1 = open('test.txt','r+')
new_file = 'new_test.txt'

if not os.path.exists(new_file):
os.system(r"touch {}".format(new_file))
file2 = open('new_test.txt','r+')
istxt = re.compile(r'.*if.*',re.I)
for line in file1.readlines():
line = line.strip()
ifstr = re.findall(istxt,line)
if ifstr:
line = []
else:
file2.write(line + '\n')

file1.close()
file2.close()
#重命名文件
os.rename("new_test.txt","test.txt")

閱讀全文

與python刪除txt文本某行相關的資料

熱點內容
伺服器有什麼危害 瀏覽:256
飢荒怎麼開新的獨立伺服器 瀏覽:753
文件夾變成了 瀏覽:560
linuxpython綠色版 瀏覽:431
怎麼下載小愛同學音箱app 瀏覽:554
python佔位符作用 瀏覽:76
javajdbcpdf 瀏覽:543
php網頁模板下載 瀏覽:192
python試講課pygame 瀏覽:409
安居客的文件夾名稱 瀏覽:677
家裡伺服器如何玩 瀏覽:451
網站源碼使用視頻 瀏覽:748
stc89c52單片機最小系統 瀏覽:452
郵件安全證書加密 瀏覽:416
雲伺服器如何訪問百度 瀏覽:279
常州電信伺服器dns地址 瀏覽:839
用小方塊製作解壓方塊 瀏覽:42
圖像壓縮編碼實現 瀏覽:68
特色功能高拋低吸線副圖指標源碼 瀏覽:71
西方哲學史pdf羅素 瀏覽:874