working solution:
import re
x = open(r'F:\1\xxx .txt')
Lines = x.readlines()
repldict = {'zzz1':(Lines[0]).strip(), 'zzz2':(Lines[1]).strip()}
def replfunc(match) :
return repldict[match.group(0)]
regex = re.compile('|'. join(re.escape( x) for x in repldict))
with open(r'F:\1\xxx .txt') as fin, open(r'F:\1\xxx 2.txt','w') as fout:
for line in fin:
f...
User Profile
Collapse
-
search and replace with line in document
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\xxx 2.txt', 'w').write(new_ str)
in xxx1 document i have written for testing:
1
2
3
4
5
6... -
found this software by the way which makes it really easy if its relevant to anyone- nodesoft-searchandreaplc eLeave a comment:
-
search and replace in python. replacing content between markers
so i have a code in python for search and replace.
fin = open(r'F:\1\xxx .txt')
fout = open(r'F:\1\xxx 2.txt', "wt")
for line in fin:
fout.write( line.replace('f oo', 'bar') )
fin.close()
fout.close()
and I was wondering if there was a way to tell it to replace anything between a certain set of markers. like anything between the number 1111 and 2222 if they appear in the... -
chickflick91 replied to search and replace first amount of strings instances with one thing and a second amouin Pythonsolved by code
get_all_index = lambda s : [i for i,_ in enumerate(lines ) if i-len(s)+1 >= 0 and lines[i-len(s)+1:i+1] == s]
with open(r'F:\1\xxx .txt') as fin:
lines = fin.read()
# get a list of all instances of number 1
all_index = get_all_index(' yyyy1')
# convert the file string into a list
list_lines = list(lines)
# replace the first 10 instances with 2;
# then the...Leave a comment:
-
chickflick91 replied to search and replace first amount of strings instances with one thing and a second amouin Pythonso i was given the code
with open(r'F:\1\xxx .txt') as fin:
lines = fin.read()
# get a list of all instances of number 1
all_index = [i for i,x in enumerate(lines ) if i != 0 and lines[i-1:i+1] == 'x1']
# convert the file string into a list
list_lines = list(lines)
# replace the first 10 instances with 2;
# then the second 10 instances with 3
for i in all_index[:10]:
list_l...Leave a comment:
-
chickflick91 replied to search and replace first amount of strings instances with one thing and a second amouin Pythondidnt work for some reason. i tested putting in xxx.txt
1111111111
1111111111
and i got
2222222222
2222222222
in xxx2.txtLeave a comment:
-
chickflick91 started a topic search and replace first amount of strings instances with one thing and a second amouin Pythonsearch and replace first amount of strings instances with one thing and a second amou
i have a code in python to search and replace what i need though is to replace the first say 10 instances of the number 1 with 2 and the second 10 instances with the number 3. anybody knows how to do that?
fin = open(r'F:\1\xxx .txt')
fout = open(r'F:\1\xxx 2.txt', "wt")
for line in fin:
fout.write( line.replace('1 ', '2') )
fin.close()
fout.close()
No activity results to display
Show More
Leave a comment: