Remove text after occurence of any character in each line

Do you need to remove some text in each line after some character or characters or separator. Below script will do this. This is a example:

Original Text:

Date 24 July : Done
Date 25 July :Pending
Date 26 July : Done
Date 27 July : Done
Date 28 July : Pending

Output Text:

If separator is given as ':'  

Date 24 July
Date 25 July
Date 26 July
Date 27 July
Date 28 July


Script:

import subprocess
import fileinput
import sys


#change default separator here
sepe = raw_input('Enter the seperator ') or '|'


open('mytext.txt', 'w').close()
subprocess.call(['notepad.exe', 'mytext.txt'])
for line in fileinput.input('mytext.txt', inplace=True):
    tmpline = line.split(sepe)
    line = tmpline[0] + '\n'
    sys.stdout.write(line)
subprocess.call(['notepad.exe', 'mytext.txt'])


================================================================

Notes:

1. Anything can be given as separator, even two-three characters like 'July' in above example.

2. 'or' operator in the raw_input value assignment declares the default value of sepe in case no value is entered by user. You can chance '|' to anything that you use most, so that you will not have to input it all the times.

Comments