-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace.py
62 lines (49 loc) · 1.23 KB
/
replace.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# improvement make an option for partial match to replace a substring in the column
import argparse
parser=argparse.ArgumentParser(description='replace a string in the files')
parser.add_argument(
'-i',
'--input',
type=str,
help='Path for the input file'
)
parser.add_argument(
'-o',
'--output',
type=str,
help='Path for the output file'
)
parser.add_argument(
'-r',
'--replace',
type=str,
help='replace string'
)
parser.add_argument(
'-w',
'--replace_with',
help='string to replace with'
)
args=parser.parse_args()
replace_with=args.replace_with
input=args.input
output=args.output if args.output else 'outputFile.txt'
replace=args.replace
def replaceWhole(input, output, replace, replace_with):
inputFile=open(input, 'r')
outputFile=open(output, 'w')
for line in inputFile:
if replace_with==r'\t':
outArg='\t'
else:
outArg=replace_with
if replace==r'\t':
inArg='\t'
else:
inArg=replace
outputLine=line.replace(inArg, outArg)
outputFile.write(outputLine)
inputFile.close()
outputFile.close()
if __name__=='__main__':
replaceWhole(input, output, replace, replace_with)