Python - Remove entire Line in file using specific data -


i'm here because exhausted resources built program...

i want delete line of file using specific data this

code \t name \t colum2 \t colum3 \t colum4 \t colum5 \n

i want delete entire line based on code

if type code, *.py search line thats contains string , delete it.

can soul me?

assuming following input (generated on generatedata.com) , saved input.txt:

1    jennifer o. ingram    p.o. box 724, 4252 arcu. st. 2    lacy n. fields    5998 scelerisque road 3    blythe p. abbott    ap #251-2931 magna. rd. 4    alyssa y. cobb    438-8110 enim. rd. 5    peter z. may    ap #271-8340 eget avenue 6    mackenzie a. santos    8366 nunc. st. 7    kevyn c. willis    ap #583-9635 erat avenue 8    nissim e. ward    7606 duis rd. 9    duncan j. armstrong    ap #164-282 id, st. 10    jesse b. barnett    p.o. box 742, 5758 sagittis street 

the following code remove line code 5:

# declare code want delete. # can further improved being parameter # or read outside script. code = 5 removed_lines = 0 f = open("input.txt","r+") lines = f.readlines() f.seek(0) line in lines:     # writes file lines except     # begin code declared above.     if not line.startswith(str(code)):         f.write(line)     else:         print("removed line %s" % line)         removed_lines += 1 f.truncate() f.close() print("%d lines removed" % removed_lines) 

and input.txt be:

1    jennifer o. ingram    p.o. box 724, 4252 arcu. st. 2    lacy n. fields    5998 scelerisque road 3    blythe p. abbott    ap #251-2931 magna. rd. 4    alyssa y. cobb    438-8110 enim. rd. 6    mackenzie a. santos    8366 nunc. st. 7    kevyn c. willis    ap #583-9635 erat avenue 8    nissim e. ward    7606 duis rd. 9    duncan j. armstrong    ap #164-282 id, st. 10    jesse b. barnett    p.o. box 742, 5758 sagittis street 

if files large, run of script can take more time , resources.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -