python刪除文件某一行
❶ python中怎麼刪除文件中指定的行
刪除文件的某一行,可以跳過你要刪除的行進行讀寫,如:
1
2
3
4
data = open(filename, 'rt').readlines()
with open(filename, 'wt') as handle:
handle.writelines(data[:tobedeleted])
handle.writelines(data[tobedeleted+1:])
其中data是逐行讀取文件,
handle.writelines進行讀寫,跳過tobedeleted行
❷ Python中刪除文檔中的固定行
import re
list = []
matchPattern = re.compile(r'.+:\sdana') #簡陋的reg用來匹配包含'dana'的那行
file = open('source.txt','r') #假設'source.txt'是你要處理的文檔
while 1:
line = file.readline() #從文件中讀出一行
if not line: #如果讀出的是文件結尾,就退出循環
break
elif matchPattern.search(line): #如果讀出的行匹配上了定義的reg,那啥也不做
pass
else: #如果讀出的行不匹配reg,也不是文件尾,那就
list.append(line) #把這行存入list列表。
file.close()
file = open('target.txt', 'w') #重新打開一個文件,把list列表裡面的內容
for i in list: #逐行寫入。最後'target.txt'文件就是不包含
file.write(i) #'dana'那行的文件
file.close()
❸ python 讀取文本文件 刪除里邊的空行
Python讀取一個文本文件,刪除文本文件的空行代碼如下:
defdelblankline(infile,outfile):
"""Deleteblanklinesofinfile"""
infp=open(infile,"r")o
utfp=open(outfile,"w")
lines=infp.readlines()
forliinlines:
ifli.split():
outfp.writelines(li)
infp.close()
outfp.close()
#調用示例
if
__name__=="__main__":
delblankline("1.txt","2.txt")
❹ 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執行文件讀寫時,怎樣刪除文件某一固定行
你是怎麼讀這個文件的,如果是x=f.readlines()
直接del x[1] 就可以了
如果是迭代器,當迭代到1(第2行)時跳過。
❻ python刪除文件中指定內容
下面代碼假定你是要刪掉20150723開頭的行:
lines=[lforlinopen("file.txt","r")ifl.find("20150723",0,8)!=0]
fd=open("file.txt","w")
fd.writelines(lines)
❼ python 刪除特定幾行
你之前編寫的Python直接用記事本打開,然後刪除保存就好了呀~
❽ python如何批量對文件夾里所有excel特定行進行刪除
path是文件夾的路徑,num是個數組,代表你要刪除的行,把要刪除的行寫進去
❾ 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 打開某個文件 刪除指定行
是不是想要直接打開文件夾,那使用如下命令就可以
import
os
os.system('explorer.exe
/n,
文件夾路徑')
這樣就可以直接打開,要打開文件,不知道你是想在程序里讀還是直接開文件,開文件用
os.system('cmd
/c
文件名')
在腳本中使用文件內容,那就使用open函數來讀取文件內容。