Question

I have a text file named text.txt

content of 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

I need to append a text(which i call as description of the profile) after every name.

Example: In the 1st line "1. Luv_dev" I want to append "<- He's a developer" after it, because it contains the keyword "_dev".

Similarly, for 3rd line "3. Sandeep_supp" i want to append "<- He's a support guy" after it, because it contains the keyword "_supp".

So bottom line i want my text file to be something like this:

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

I have started doing it but i don't think i'm on the right track of achieving my goal.

 #!/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")

this code is not giving me anything :(

Was it helpful?

Solution

One of your issues is you are attempting to read from a file which is opened for writing. This is not possible. You need to read from one file, and write to another. The below code uses the with-statement to open the input file and an output file.

You don't need regular expressions here. You can simply check if the line ends with either dev or supp and append the text you want accordingly. For that, use the 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))

Your python version is six years old. You should consider updating to at least python 2.7.x but preferrably to python 3.x. The with-statement is not available in python 2.4. You have to open and close the files manually:

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()

Outputs to 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$ 

OTHER TIPS

I have my code working as well :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()

Thank you all for your answers :)

testing by line.rsplit()

There are multiple methods how to test the role, one bing rsplit, which takes defined string as delimiter and starts splitting the line from the right side as many times as specified in next parameter.

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

I also changed the logic to find full role name from dictionary.

In case the role is not present, "unknown role" is used as default.

#!/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()))

In case you are found of older versions of Python not having any idea what string.format is about, change the line

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

into

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

This will work with recent Python too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top