Python搜索行中的字符串

Python搜索行中的字符串,python,string,replace,substitution,Python,String,Replace,Substitution,我有以下代码,我认为应该可以工作,但似乎没有: old_name = 'Some User' new_name = 'New User' with open(complete_filename, 'r') as provisioning_file: lines = provisioning_file.read() # if the old_name is in this file if old_name in lines: file_found = 1 w

我有以下代码,我认为应该可以工作,但似乎没有:

old_name = 'Some User'
new_name = 'New User'

with open(complete_filename, 'r') as provisioning_file:
   lines = provisioning_file.read()
   # if the old_name is in this file
   if old_name in lines:
     file_found = 1
     with open(complete_filename + '.new', 'w') as new_provisioning_file:
       for line in lines:
         line = line.replace(old_name, new_name)
         new_provisioning_file.write(line)
         # provisioning_file.write(re.sub(old_name, new_name, line))
文件
complete\u filename
将是各种各样的配置文件,我已经用一些XML文件进行了测试,下面是其中一个示例片段:

  <reg reg.1.address="1234" reg.1.label="Some User" >
        <reg.1.auth reg.1.auth.password="XXXXXXXXXX" reg.1.auth.userId="1234" />
        <reg.1.outboundProxy reg.1.outboundProxy.address="sip.example.com" />
        <reg.1.server reg.1.server.1.address="sip.example.com" reg.1.server.1.expires="300" reg.1.server.2.expires="300" />
        <reg.1.serverFeatureControl reg.1.serverFeatureControl.dnd="0" />
     </reg>

代码找到
old_name
字符串并进入
if
语句,然后打开
complete_filename.new
进行写入,但它显然从未在行中找到旧名称,只是按原样输出文件(即它不会用
new_name
替换
old_name

从代码中可以看出,我还使用re.sub进行了类似的实验。我错过了什么

lines = provisioning_file.read()
我觉得这不对劲
read()
不返回行列表,它返回单个字符串。因此,稍后当您对行中的行执行
时:
,您不是逐行迭代,而是一次迭代一个字符

在迭代对象之前,尝试拆分对象。我还建议更改其名称,以便更好地描述其内容

with open(complete_filename, 'r') as provisioning_file:
   text= provisioning_file.read()
   # if the old_name is in this file
   if old_name in text:
     file_found = 1
     with open(complete_filename + '.new', 'w') as new_provisioning_file:
       for line in text.split("\n"):
         line = line.replace(old_name, new_name)
         new_provisioning_file.write(line + "\n")

编辑:替代方法:

old_name = 'Some User'
new_name = 'New User'

with open(complete_filename, 'r') as provisioning_file:
   lines = provisioning_file.readlines()
   # if the old_name is in this file
   if any(old_name in line for line in lines):
     file_found = 1
     with open(complete_filename + '.new', 'w') as new_provisioning_file:
       for line in lines:
         line = line.replace(old_name, new_name)
         new_provisioning_file.write(line)
我觉得这不对劲
read()
不返回行列表,它返回单个字符串。因此,稍后当您对行中的行执行
时:
,您不是逐行迭代,而是一次迭代一个字符

在迭代对象之前,尝试拆分对象。我还建议更改其名称,以便更好地描述其内容

with open(complete_filename, 'r') as provisioning_file:
   text= provisioning_file.read()
   # if the old_name is in this file
   if old_name in text:
     file_found = 1
     with open(complete_filename + '.new', 'w') as new_provisioning_file:
       for line in text.split("\n"):
         line = line.replace(old_name, new_name)
         new_provisioning_file.write(line + "\n")

编辑:替代方法:

old_name = 'Some User'
new_name = 'New User'

with open(complete_filename, 'r') as provisioning_file:
   lines = provisioning_file.readlines()
   # if the old_name is in this file
   if any(old_name in line for line in lines):
     file_found = 1
     with open(complete_filename + '.new', 'w') as new_provisioning_file:
       for line in lines:
         line = line.replace(old_name, new_name)
         new_provisioning_file.write(line)

谢谢这是有道理的。我想另一种选择是
lines=provisioning\u file.readlines()
?这是另一种解决问题的方法吗?然后像我一样迭代。是的,事实上这可能更可取,因为这样您就不必在
write
调用中手动写入换行符。但是您必须将条件修改为
(行中的旧名称对应行中的行)
,因为
中的
不会对列表的内容进行部分匹配。(例如,
“a”在[“ab”,“cd”]
中的计算结果为False)我尝试了此操作,但在条件上出现语法错误,您是否有可能在上下文中的片段中向我显示此内容?当然,已编辑。也许是因为我忘了冒号?谢谢!这是有道理的。我想另一种选择是
lines=provisioning\u file.readlines()
?这是另一种解决问题的方法吗?然后像我一样迭代。是的,事实上这可能更可取,因为这样您就不必在
write
调用中手动写入换行符。但是您必须将条件修改为
(行中的旧名称对应行中的行)
,因为
中的
不会对列表的内容进行部分匹配。(例如,
“a”在[“ab”,“cd”]
中的计算结果为False)我尝试了此操作,但在条件上出现语法错误,您是否有可能在上下文中的片段中向我显示此内容?当然,已编辑。也许是因为我忘了冒号?