I'm new to python,I wanted to replace text or more specifically words in a file using the list in excel. So,was doing my research and wrote the code. But I'm stuck at a place.
First was trying to replace in same file :-
for line in file:
i=0
for x in range(nrows):
file.write(line.replace(sheet1.cell(i,0).value,sheet1.cell(i,1).value))
#print(string)
i=i+1
file.close()
It was replacing but also replicating the line multiple times.
So I researched and found other way that was writing in Two different files and with a string variable.
Here is the code for it :-
for line in file:
i=0
string=line
for x in range(nrows):
string.replace(sheet1.cell(i,0).value,sheet1.cell(i,1).value)
#print(string)
i=i+1
file1.write(string)
#print(string)
But Now It's not replacing anything just copy pasting the code.
Can anyone pls guide me with both ways ....
So as per the post https://stackoverflow.com/questions/5453267/is-it-possible-to-modify-lines-in-a-file-in-place
I have Inculcated the fileinput function
But after running the script the file becomes blank
Can you pls tell me...
My Updated Code :-
for line in fileinput.input(filepath,inplace=True, backup='.bak'):
i=0
for x in range(nrows):
line.replace(sheet1.cell(i,0).value,sheet1.cell(i,1).value)
#print(string)
i=i+1