我想删除文件中的某些特定行。以下代码似乎不起作用。没有抛出任何错误,但此代码不会删除要删除的行。
#!/usr/bin/python
import argparse
import re
import string
p = argparse.ArgumentParser()
p.add_argument("input", help="input the data in format ip:port:name", nargs='*')
args = p.parse_args()
kkk_list = args.input # ['1.1.1.1:443:myname', '2.2.2.2:443:yourname']
def getStringInFormat(ip, port, name):
formattedText = "HOST Address:{ip}:PORT:{port}\n"\
" server tcp\n"\
" server {ip}:{port} name {name}\n\n".format(ip=ip,
port=port,
name=name)
return formattedText
with open("file.txt", "r+") as f:
fileContent = f.read()
# below two lines delete old content of file
f.seek(0)
f.truncate()
for kkk in kkk_list:
ip, port, name = re.split(":|,", kkk)
stringNeedsToBeDeleted = getStringInFormat(ip, port, name)
fileContent = fileContent.replace(stringNeedsToBeDeleted, "")
f.write(fileContent)
我尝试删除的文件内容如下所示。请注意第2行和第3行之前的空格
------------do not delete this line------
HOST Address:1.1.1.1:PORT:443\n"\
server tcp\n"\
server 1.1.1.1:443 name myname1
--------------- do not delete this line either
如果脚本成功,文件应该如下所示,其间只有一个新行。
------------do not delete this line------
------------do not delete this line either ----
任何见解?
答案 0 :(得分:1)
你正在文件编辑循环中正确地做所有事情,这意味着如果你实际上没有替换任何东西,因为你正在寻找的字符串不存在。的确,当你告诉我们你正在寻找这个字符串时:
------------do not delete this line------
HOST Address:1.1.1.1:PORT:443\n"\
server tcp\n"\
server 1.1.1.1:443 name myname1
--------------- do not delete this line either
它似乎与您尝试将其匹配的字符串匹配:
formattedText = "HOST Address:{ip}:PORT:{port}\n"\
" server tcp\n"\
" server {ip}:{port} name {name}\n\n"
请记住,为了用当前代码替换此字符串,字符串必须完全匹配,在这种情况下,我不会在" HOST之间看到\ n Addess ..."和" server tcp \ n" \或\ lines。但我怀疑那些只是你的格式错误。
如果你真的想找到这个问题的根源,我建议你找一个你确定要删除的字符串并用它来测试你的代码,以确保字符串是相同的。这是一个例子。如果你想找到:
HOST Address:1.1.1.1:PORT:443
server tcp
server 1.1.1.1:443 name myname1
然后通过以下方式与您的搜索字符串进行比较:
test_string = # the string I posted above, you should probably
# grab this from a file for consistency.
kkk = '1.1.1.1:443:myname'
ip, port, name = re.split(":|,", kkk)
assert ip == '1.1.1.1'
assert port == '443'
assert name == 'myname'
stringNeedsToBeDeleted = getStringInFormat(ip, port, name)
assert stringNeedsToBeDeleted == test_string, "Error, strings are not equal!"
这应该可以为您提供实际问题的线索。我直接从您的示例中抓取的myname1
与您的匹配字符串不匹配。
答案 1 :(得分:0)
您可以将文件的内容复制到列表中,写入旧文件,改变列表,然后将列表写入文件。
import argparse
import re
import string
p = argparse.ArgumentParser()
p.add_argument("input", help="input the data in format ip:port:name",nargs='*')
args = p.parse_args()
kkk_list = args.input # ['1.1.1.1:443:myname', '2.2.2.2:443:yourname']
def getStringInFormat(ip, port, name):
formattedText = "HOST Address:{ip}:PORT:{port}\n"\
" server tcp\n"\
" server {ip}:{port} name {name}\n\n".format(ip=ip,
port=port,
name=name)
return formattedText
with open("file.txt", "r+") as f:
for kkk in kkk_list:
ip, port, name = re.split(":|,", kkk)
stringNeedsToBeDeleted = getStringInFormat(ip, port, name)
fileContent = fileContent.replace(stringNeedsToBeDeleted, "")
f.write(fileContent) #now, your file contains all the host addressed and IPs
f = open("file.txt").readlines()
contents = [i.strip('\n').split() for i in f]
new_file = open('file.txt', 'w')
new_file.write('')
new_file.write(''.join(contents[0]))
new_file.write('\n\n\n')
new_file.write(contents[''.join(len(contents)-1])))
new_file.close()