搜索包含一些文本的行,替换完整行 python

发布于 2024-12-10 09:04:24 字数 315 浏览 1 评论 0原文

所以我试图搜索某个字符串,例如可能是: process.control.timeout=30,但 30 可以是任何值。我尝试了这个:

for line in process:
    line = line.replace("process.control.timeout", "process.control.timeout=900")
    outFile.write(line)

但这会让我回来 process.control.timeout=900=30。我有一种感觉,我需要将正则表达式与通配符一起使用?但我对 python 还很陌生。

So I am trying to search for a certain string which for example could be:
process.control.timeout=30, but the 30 could be anything. I tried this:

for line in process:
    line = line.replace("process.control.timeout", "process.control.timeout=900")
    outFile.write(line)

But this will bring me back process.control.timeout=900=30. I have a feeling I will need to use regex with a wildcard? But I'm pretty new to python.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

囍孤女 2024-12-17 09:04:24

没有正则表达式:

for line in process:
    if "process.control.timeout" in line:
        # You need to include a newline if you're replacing the whole line
        line = "process.control.timeout=900\n" 
    outFile.write(line)

或者

outFile.writelines("process.control.timeout=900\n" 
                      if "process.control.timeout" in line else line 
                         for line in process)

如果您要匹配的文本位于行的开头,请使用 line.startswith("process.control.timeout") 而不是 "process.control.timeout “在行

Without regex:

for line in process:
    if "process.control.timeout" in line:
        # You need to include a newline if you're replacing the whole line
        line = "process.control.timeout=900\n" 
    outFile.write(line)

or

outFile.writelines("process.control.timeout=900\n" 
                      if "process.control.timeout" in line else line 
                         for line in process)

If the text you're matching is at the beginning of the line, use line.startswith("process.control.timeout") instead of "process.control.timeout" in line.

你是年少的欢喜 2024-12-17 09:04:24

你是对的,正则表达式是正确的选择。

import re
pattern = re.compile(r"process\.control\.timeout=\d+")
for line in process:
    line = pattern.sub("process.control.timeout=900", line)
    outFile.write(line)

You are correct, regex is the way to go.

import re
pattern = re.compile(r"process\.control\.timeout=\d+")
for line in process:
    line = pattern.sub("process.control.timeout=900", line)
    outFile.write(line)
且行且努力 2024-12-17 09:04:24

这可能就是您想要的(匹配 == 后面的数字是可选的)。当您在循环中搜索和替换时,单独编译正则表达式比直接使用 re.sub 更有效。

import re

pattern = re.compile(r'process\.control\.timeout(=\d+)?')

for line in process:
    pattern.sub('process.control.timeout=900', line)

This is probably what you want (matching = and digits after = is optional). As you are searching and replacing in a loop, compiling the regex separately will be more efficient than using re.sub directly.

import re

pattern = re.compile(r'process\.control\.timeout(=\d+)?')

for line in process:
    pattern.sub('process.control.timeout=900', line)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文