如果找到,则替换行或追加-python
我的文本是由“=”分隔的键值对。如果钥匙匹配,我想更换线路。如果没有,我想把它附加在底部。我尝试了几种方法,包括:如果找到,则替换行或追加-python,python,Python,我的文本是由“=”分隔的键值对。如果钥匙匹配,我想更换线路。如果没有,我想把它附加在底部。我尝试了几种方法,包括: def split_command_key_and_value(command): if '=' in command: command2 = command.split('=') return command2 def test(command, path): command2 = split_command_key_and_value(co
def split_command_key_and_value(command):
if '=' in command:
command2 = command.split('=')
return command2
def test(command, path):
command2 = split_command_key_and_value(command)
pattern = command2[0]
myfile = open(path,'r') # open file handle for read
# use r'', you don't need to replace '\' with '/'
result = open(path, 'w') # open file handle for write
for line in myfile:
line = line.strip() # it's always a good behave to strip what you read from files
if pattern in line:
line = command # if match, replace line
result.write(line) # write every line
myfile.close() # don't forget to close file handle
result.close()
我知道上面只是为了替换文本,但它删除了文件中的文本,我不明白为什么。有人能给我指出正确的方向吗
谢谢
更新:
我快到了,但是我的一些行有相似的键,所以当只有1行应该匹配时,多行是匹配的。我尝试在循环中加入正则表达式边界,但没有成功。我的代码如下。有人有什么建议吗
文件中有一些文本不是键值,所以我想跳过它
def modify(self, name, value):
comb = name + ' ' + '=' + ' ' + value + '\n'
with open('/file/', 'w') as tmpstream:
with open('/file/', 'r') as stream:
for line in stream:
if setting_name in line:
tmpstream.write(comb)
else:
tmpstream.write(line)
我想我明白了。请参阅下面的代码
def modify(self, name, value):
comb = name + ' ' + '=' + ' ' + value + '\n'
mylist = []
with open('/file/', 'w') as tmpstream:
with open('/file/', 'r') as stream:
for line in stream:
a = line.split()
b = re.compile('\\b'+name+'\\b')
if len(a) > 0:
if b.search(a[0]):
tmpstream.write(comb)
else:
tmpstream.write(line)
我说得太快了。它在我提供的键值处停止。所以,它只写一行,不写不匹配的行
def modify(name, value):
comb = name + ' ' + '=' + ' ' + value + '\n'
mylist = []
with open('/file1', 'w') as tmpstream:
with open('/file2', 'r') as stream:
for line in stream:
a = line.split()
b = re.compile('\\b'+name+'\\b')
if len(a) > 0:
if b.search(a[0]):
tmpstream.write(comb)
else:
tmpstream.write(line)
有人能看到这个问题吗?因为当您打开文件进行写入时
result = open(path, 'w') # open file handle for write
你只要删除它的内容。尝试写入不同的文件,完成所有工作后,用新文件替换旧文件。或者将所有数据读入内存,然后进行处理并写入文件
with open(path) as f:
data = f.read()
with open(path, 'w') as f:
for l in data:
# make job here
首先,你正在读一篇文章,写同一个文件。。。 你可以先把它全部读一遍,然后逐行写下来
with open(path,'r') as f:
myfile = f.read() # read everything in the variable "myfile"
result = open(path, 'w') # open file handle for write
for line in myfile.splitlines(): # process the original file content 1 line at a time
# as before
我强烈建议您阅读python关于如何使用的文档 如果以写入模式打开现有文件
open(路径“w”)
,其内容将被擦除:
模式可以是(…)“w”仅用于写入(具有相同名称的现有文件将被擦除)
要替换python中的一行,可以查看以下内容:
这里有一个适合您环境的解决方案(针对python3进行了测试):
请注意,使用上述代码,将替换与key(key=blob或blob=key)匹配的每一行。您能提供一个输入示例吗?
from tempfile import mkstemp
from shutil import move
from os import close
def test(filepath, command):
# Split command into key/value
key, _ = command.split('=')
matched_key = False
# Create a temporary file
fh, tmp_absolute_path = mkstemp()
with open(tmp_absolute_path, 'w') as tmp_stream:
with open(filepath, 'r') as stream:
for line in stream:
if key in line:
matched_key = True
tmp_stream.write(command + '\n')
else:
tmp_stream.write(line)
if not matched_key:
tmp_stream.write(command + '\n')
close(fh)
move(tmp_absolute_path, filepath)