Python:将长字符串与特殊字符和空格匹配,然后在开头添加两个字符

发布于 2025-01-08 16:32:21 字数 674 浏览 1 评论 0原文

我不知道如何解决这个问题,我试图在一个有大量空格和特殊字符的文本文件中匹配这个长字符串,并将这些字符附加到前面,即。 “//”

我需要匹配这一行:

$menu_items['gojo_project']                    => array('http://www.gojo.net/community/plugin-inventory/ops-gojo/gojo', 'gojo',3),

并将其变成这样:

//$menu_items['gojo_project']                    => array('http://www.gojo.net/community/plugin-inventory/ops-gojo/gojo', 'gojo',3),

注意我只是在前面添加了两个“/”字符。

我尝试使用 re.escape 来格式化字符串,但它真的很长并且仍然抛出语法错误。我使用“re”以正确的方式处理这个问题吗?或者是否有一种更好的Pythonic方法来匹配文本文件中这样的字符串并添加到它前面?

编辑:忘记提及我需要在线编辑文件。简而言之,它是一个很长的 php 脚本,我试图找到该行并将其注释掉(即 //)。所以,我不能真正使用一些建议的解决方案(我认为),因为他们将修改写入单独的文件。

I'm not sure how to approach this, i'm trying to match this long string in a text file that has lots of whitespace and special characters and append the characters to the front ie. "//"

i need to match this line:

$menu_items['gojo_project']                    => array('http://www.gojo.net/community/plugin-inventory/ops-gojo/gojo', 'gojo',3),

and turn it into this:

//$menu_items['gojo_project']                    => array('http://www.gojo.net/community/plugin-inventory/ops-gojo/gojo', 'gojo',3),

notice i just prepended two '/' character.

I tried using re.escape to format the string, but its just really long and still throw sytax error. Am i going about this the right way using 're' ? or is there a better pythonic way to match a string like this one in a text file and prepend to it?

Edit: Forgot to mention that i need to edit the file in-line. In short, its a long php script that i'm trying to find that line and comment it out (ie. //). So, I cant really use some of the proposed solutions(i think) since they have it writing the modification to a separate file.

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

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

发布评论

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

评论(2

尴尬癌患者 2025-01-15 16:32:21

尝试 fileinput 它可以让你读取文件并就地重写行:

import fileinput

for line in fileinput.input("myfile.txt", inplace = 1):
    if line == "$menu_items['gojo_project']                    => array('http://www.gojo.net/community/plugin-inventory/ops-gojo/gojo', 'gojo',3),":
        line = '//' + line
    print line,

Try fileinput it will let you read over a file and rewrite lines in place:

import fileinput

for line in fileinput.input("myfile.txt", inplace = 1):
    if line == "$menu_items['gojo_project']                    => array('http://www.gojo.net/community/plugin-inventory/ops-gojo/gojo', 'gojo',3),":
        line = '//' + line
    print line,
梦与时光遇 2025-01-15 16:32:21

如果您尝试精确匹配该字符串,那么使用字符串相等运算符而不是正则表达式会更容易。

longString = "$menu_items['gojo_project']                    => array('http://www.gojo.net/community/plugin-inventory/ops-gojo/gojo', 'gojo',3),"

input = open("myTextFile.txt", "r")
output = open("myOutput.txt", "w")
for line in input:
    if line.rstrip() == longString: #rstrip removes the trailing newline/carriage return
        line = "//" + line
    output.write(line)
input.close()
output.close()

If you're trying to match exactly that string, it would be easier just to use the string equality operator, rather than regular expressions.

longString = "$menu_items['gojo_project']                    => array('http://www.gojo.net/community/plugin-inventory/ops-gojo/gojo', 'gojo',3),"

input = open("myTextFile.txt", "r")
output = open("myOutput.txt", "w")
for line in input:
    if line.rstrip() == longString: #rstrip removes the trailing newline/carriage return
        line = "//" + line
    output.write(line)
input.close()
output.close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文