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

时间:2014-05-07 10:50:13

标签: python regex file append

我有一个名为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行“1. Luv_dev”中我想追加“< - 他是开发人员”,因为它包含关键字“_dev”。

同样,对于第3行“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 :(得分:1)

您的一个问题是,您正试图从为打开的文件中读取。这是不可能的。您需要从一个文件中读取,然后写入另一个文件。下面的代码使用with - 语句打开输入文件和输出文件。

这里不需要正则表达式。您只需检查该行是以dev还是supp结尾,然后相应地附加您想要的文字。为此,请使用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$ 

答案 1 :(得分:0)

line.rsplit()

进行测试

有多种方法可以测试角色,一个是rsplit,它将定义的字符串作为分隔符,并开始从右侧分割该行,与下一个参数中指定的次数相同。

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

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

如果角色不存在,&#34;未知角色&#34;用作默认值。

#!/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。

答案 2 :(得分: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()

谢谢大家的回答:)