Python 搜索行中的字符串
Python search for string in line
我有以下代码,我认为它应该可以工作,但似乎没有:
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_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()
不是 return 行列表,它 return 是一个字符串。所以稍后当你做 for line in lines:
时,你不是逐行迭代,而是一次迭代一个字符。
尝试split
迭代对象之前调整对象。我还建议更改其名称,以便更好地描述其内容。
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)
我有以下代码,我认为它应该可以工作,但似乎没有:
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_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()
不是 return 行列表,它 return 是一个字符串。所以稍后当你做 for line in lines:
时,你不是逐行迭代,而是一次迭代一个字符。
尝试split
迭代对象之前调整对象。我还建议更改其名称,以便更好地描述其内容。
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)