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函数来读取文件内容。