Oct-14-2017, 04:20 PM
I am trying to write a code that searches and replaces with a line in the document. i have:
import re
x = open(r'F:\1\xxx.txt')
string = open(r'F:\1\xxx.txt').read()
Lines = x.readlines()
new_str = re.sub('zzz1', (Lines[0]) , string)
open(r'F:\1\xxx2.txt', 'w').write(new_str)
in xxx1 document i have written for testing:
1
2
3
4
5
6
7
8
9
10
zzz1
11
but the output I get in xxx2 document is:
1
2
3
4
5
6
7
8
9
10
1
11
can i have zzz1 replaced without the empty line to follow?
and how can i do it again say if wanting to replace a zzz2 with line 2?
import re
x = open(r'F:\1\xxx.txt')
string = open(r'F:\1\xxx.txt').read()
Lines = x.readlines()
new_str = re.sub('zzz1', (Lines[0]) , string)
open(r'F:\1\xxx2.txt', 'w').write(new_str)
in xxx1 document i have written for testing:
1
2
3
4
5
6
7
8
9
10
zzz1
11
but the output I get in xxx2 document is:
1
2
3
4
5
6
7
8
9
10
1
11
can i have zzz1 replaced without the empty line to follow?
and how can i do it again say if wanting to replace a zzz2 with line 2?