在匹配模式的文件中追加文本

0 投票
3 回答
2958 浏览
提问于 2025-04-18 05:39

我有一个名为 text.txt 的文本文件。

text.txt 的内容是:

1. Luv_dev
2. Amit_dev
3. Sandeep_supp
4. Prateek_supp
5. Sumit_dev
6. Shashank_dev
7. Kush_supp
8. Ritesh_dev
9. Shubham_supp
10. Ravi_dev

我想在每个名字后面加上一段文字(我称之为个人简介)。

举个例子:在第一行 "1. Luv_dev" 后面,我想加上 "<- 他是一个开发者",因为里面有关键词 "_dev"。

同样,在第三行 "3. Sandeep_supp" 后面,我想加上 "<- 他是一个支持人员",因为里面有关键词 "_supp"。

所以,最终我希望我的文本文件看起来像这样:

1. Luv_dev <- He's a developer
2. Amit_dev <- He's a developer
3. Sandeep_supp <- He's a support guy
4. Prateek_supp <- He's a support guy
5. Sumit_dev <- He's a developer
6. Shashank_dev <- He's a developer
7. Kush_supp <- He's a support guy
8. Ritesh_dev <- He's a developer
9. Shubham_supp <- He's a support guy
10. Ravi_dev <- He's a developer

我已经开始尝试这样做,但我觉得我可能没有走在正确的路上来实现我的目标。

 #!/usr/bin/python

 import re

 file = open("text.txt","a")
 for line in file:
     match_for_dev = re.match(r"\d+\.\s\w+_dev$",line)
     match_for_supp = re.match(r"\d+\.\s\w+_supp$",line)
     if match_for_dev:
          file.write("<- He's a developer")
     if match_for_supp:
          file.write("<- He's a support guy")

这段代码没有给我任何结果 :(

3 个回答

0

通过 line.rsplit() 进行测试

有很多方法可以测试角色,其中一种是 rsplit。这个方法需要你指定一个字符串作为分隔符,然后从右边开始按照你指定的次数进行分割。

>>> line "name_surname_role".rsplit("_", 1)
["name_surname", "role"]

我还修改了从字典中找到完整角色名称的逻辑。

如果找不到角色,就会使用“未知角色”作为默认值。

#!/usr/bin/python
fname = "text.txt"
outfname = "outtext.txt"
roles = {"dev": "developer", "supp": "support guy"}
with open(fname, "r") as in_f, open(outfname, "w") as out_f:
    for line in in_f:
        line = line.strip()
        role = line.rsplit("_", 1)[-1]
        print role
        rolename = roles.get(role, "unknown role")
        out_f.write("{line} <- He's a {rolename}\n".format(**locals()))

如果你使用的是较旧版本的Python,对 string.format 不太了解,可以把这一行

    out_f.write("{line} <- He's a {rolename}\n".format(**locals()))

改成

    out_f.write("%s <- He's a %s\n" % (line, rolename))

这样在最近的Python版本中也能正常工作。

0

我的代码也运行得很好 :D

#!/usr/bin/python

import re

input = open("text.txt","r")
output = open("out.txt","w")
for line in input:
    match_for_dev = re.search(r"\d+\.\s\w+_dev$",line)
    match_for_supp = re.search(r"\d+\.\s\w+_supp$",line)
    if match_for_dev:
            output.write("%s <- He's a developer\n" % match_for_dev.group(0))
    if match_for_supp:
            output.write("%s <- He's a support guy\n" % match_for_supp.group(0))

input.close()
output.close()

谢谢大家的回答 :)

1

你遇到的问题之一是,你试图从一个打开用于写入的文件中读取内容。这是不可能的。你需要从一个文件中读取数据,然后写入到另一个文件中。下面的代码使用了with语句来打开输入文件和输出文件。

在这里你不需要使用正则表达式。你只需要检查这一行是否以devsupp结尾,然后根据需要添加你想要的文本。为此,可以使用str.endswith()

with open("text.txt", "r") as inp, open("out.txt", "w") as output:
   for line in inp:
       l = line.strip()
       if l.endswith("dev"):
           output.write("{} <- He's a developer\n".format(l))
       if l.endswith("supp"):
           output.write("{} <- He's a support guy\n".format(l))

你的Python版本已经六年没更新了。你应该考虑至少更新到Python 2.7.x,但最好是更新到Python 3.x。with语句在Python 2.4中是不可用的。你需要手动打开和关闭文件:

inp = open("text.txt", "r")
output = open("out.txt", "w")

for line in inp:
    l = line.strip()
    if l.endswith("dev"):
       output.write("%s <- He's a developer\n" % l)
    if l.endswith("supp"):
       output.write("%s <- He's a support guy\n" % l)

inp.close()
output.close()

输出到out.txt:

msvalkon@Lunkwill:/tmp$ cat out.txt 
1. Luv_dev <- He's a developer
2. Amit_dev <- He's a developer
3. Sandeep_supp <- He's a support guy
4. Prateek_supp <- He's a support guy
5. Sumit_dev <- He's a developer
6. Shashank_dev <- He's a developer
7. Kush_supp <- He's a support guy
8. Ritesh_dev <- He's a developer
9. Shubham_supp <- He's a support guy
10. Ravi_dev <- He's a developer
msvalkon@Lunkwill:/tmp$ 

撰写回答