【python】⽂本处理:删除包含关键词的⾏、删除指定列、删
除指定字符、替换关键字……
⽬录
1、⾏处理
删除⽂件中包含关键词的⾏
带中⽂
删除匹配“⽉”的⾏
import re
lineList = []
matchPattern = repile(r'⽉')
file = open('D:\','r',encoding='UTF-8')
while 1:
line = adline()
if not line:
print("Read file End or Error")
break
elif matchPattern.search(line):
pass
else:
lineList.append(line)
file.close()
file = open(r'D:\', 'w',encoding='UTF-8')
for i in lineList:
file.write(i)
file.close()
---------------------------------
删除匹配“INVALID PARAMETER”的⾏
import re
list = []
matchPattern = repile(r'INVALID PARAMETER')
file = open('C:\','r')
while 1:
line = adline()
if not line:
print("Read file End or Error")
break
elif matchPattern.search(line):
pass
writelines使用方法pythonelse:
list.append(line)
file.close()
file = open(r'C:\', 'w')
for i in list:
file.write(i)
file.close()
repile(),正则表达式在模式匹配前进⾏预编译;使⽤预编译代码⽐字符串快;
<中包含以下⽂字:
1:li
2:test
3:num
在运⾏完程序,⽣成的中,内容为
1:li
3:num
删除TXT中的带/不带指定字符的⾏(并保留带指定字符的⾏)
noneed = ["null"]
need = ['{']
def isInArray (array, line):
for item in array:
if item in line:
return True
return False
fname = r'D:\download.json'
fresult = r'D:\download2.json'
#open(fname, 'r', encoding='gb2312')
with open(fname, 'r',encoding='UTF-8') as f:
with open(fresult, 'w', encoding='UTF-8') as g:
for line adlines():
if isInArray(need, line):#含need⾥⾯字符的⾏都要
g.write(line)
continue
if not isInArray(noneed, line):#不含noneed⾥⾯字符的⾏都要 g.write(line)
删除匹配or不匹配某些条件的⾏
程序框架:
f = open("",'r+')
lines = [line for line adlines() if 你对line的判断 is None]
f.seek(0)
f.writelines(lines)
f.close()
⽰例:
删除包含有 darray 内的内容的⾏
darray = [
"Entering directory",
"In function ",
"Leaving directory",
"__NR_SYSCALL_BASE",
"arm-hisiv100-linux-ar ",
"arm-hisiv100-linux-gcc ",
"but argument is of type",
"dereferencing type-punned pointer will break strict-aliasing rules", "differ in signedness",
"does break strict-aliasing rules",
"embedded '\\0' in format",
"excess elements in array initializer",
"implicit declaration of",
"make -C ",
" rm -f",
"this is the location of the previous definition",
"warning: multi-line comment"
]
def isInArray (array, line):
for item in array:
if item in line:
return True
return False
fname = r'C:\Users\YOGA\Desktop\download.json'
fresult = r'C:\Users\YOGA\Desktop\download2.json'
with open(fname, 'r', encoding='UTF-8') as f:
with open(fresult, 'w', encoding='UTF-8') as g:
for line adlines():
if not isInArray(darray, line):
g.write(line)
Python实现删除⽂件中含“指定内容”的⾏⽰例
#!/bin/env python
import shutil, sys, os
darray = [
"null",
]
def isInArray (array, line):
for item in array:
if item in line:
return True
return False
if __name__ == '__main__':
argv = sys.argv
argc = len(argv)
if argc < 2:
print("Usage: %s <file>" %(os.path.basename(argv[0])))
exit()
fname = argv[1]
fresult = fname + ".result"
with open(fname, 'r', encoding='UTF-8') as f:
with open(fresult, 'w', encoding='UTF-8') as g:
for line adlines():
if not isInArray(darray, line):
g.write(line)
2、字符处理
删除特定字符
1.1、删除特定位置的字符
使⽤.pop()⽅法。输⼊参数,即为要删除的索引。
删除第⼆个字符
string = ':⼟堆碎念'
list_str = list(string)
list_str.pop(1)
list_str = ''.join(list_str)
print(list_str)
1.2、删除指定字符
.replace()⽅法
⽐如,字符串a=':⼟堆碎念',尝试将其中的公字符删除,将公字符替换成空字符。
a= ':⼟堆碎念'
b = a.replace('公','')
count参数就可以指定要替换⼏个。我们⽆意中知道了如何删除指定数⽬的字符。
⽐如,字符串a='公公:⼟堆碎念',尝试将其中的公字符删除,将公字符替换成空字符。
a='公公:⼟堆碎念'
b = a.replace('公','',3)
b
Out[38]:'众号:⼟堆碎念'
b = a.replace('公','',2)
b
Out[38]:'众号公:⼟堆碎念'
import re
lineList = []
file = open('D:\','r',encoding='UTF-8')
while 1:
line = adline()
if not line:
print("Read file End or Error")
break
line2 = place('篇','')
lineList.append(line2)
file.close()
file = open(r'D:\', 'w',encoding='UTF-8')
for i in lineList:
file.write(i)
file.close()
1.3、删除每⼀⾏⾸/尾匹配条件的字符
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论