Reputation: 21
I have a file like this
Dog, fun= it is always, ok
cat, run = it is always, ok
I want to change the first it is always
as not always
and the second it is always
as can be always
.
My question is how to tell python to change the respective phrases as I want it to be
my code
def read_and_replace(filepath,newfilename,replacedictionary):
os.chdir(filepath)
file = open(filename, "r")
fileContent = file.read()
file.close()
for word in replacedictionary:
fileContent=fileContent.replace(word,replacedictionary[word])
output = open(filename, "w")
output.write(fileContent)
output.close()
After this I have created a dictionary with my input file in the dictionary but not having any idea of how to reach a specific line and change the text as I want it to be. I am also new to programming so stuck with this.
this replacedictionary changes both the lines with a line that I give. For example:
replacedictionary['it is always']='not always'
but once I want to change one line as "it is always" and the next line as "can be always"
Upvotes: 0
Views: 84
Reputation: 4086
Assuming you want to change the first line and only that line this is what I got:
>>> with open("test.txt", "w") as file:
file.write("Dog, fun= it is always, ok\ncat, run = it is always, ok")
54
>>> newFile = ""
>>> with open("test.txt", "r") as file:
counter = 0
for line in file:
if counter == 0:
newFile = newFile + line.replace("it is always", "not always")
elif counter == 1:
newFile = newFile + line.replace("it is always", "can be always")
else:
newFile = newFile + line
counter += 1
print(newFile)
Dog, fun= not always, ok
cat, run = it is always, ok
>>> with open("newtest.txt", "w")as newerfile:
newerfile.write(newFile)
52
>>> with open("newtest.txt", "r") as file:
for line in file:
print(line)
Dog, fun= not always, ok
cat, run = it is always, ok
This way I made the file you have. I then declared an empty string which would form our new file. I opened the original file and declared a counter. I ran through each line and, in the case that we were reading the first line, replaced the text. the counter was then incremented to show we are no longer on the first line. We then do the next replace for the second line.
Then I opened a newfile and read into that one (alternatively you could open the original file and write into the to replace everything completely). the last bit rereads the new file to make sure everything is in place.
edited to include can be always.
Upvotes: 0
Reputation: 2607
You can use string.replace()
Full example:
with open('my_file.txt') as f:
lines = f.readlines()
lines[0] = lines[0].replace('it is always', 'not always')
lines[1] = lines[1].replace('it is always', 'can be always')
print '\n'.join(lines)
>> Dog, fun= not always, ok
>> cat, run = can be always, ok
Upvotes: 0
Reputation: 7380
its not clear when exactly you want to replace the text, but replacing is easy, just use string.replace()
:
for line in lines:
line.replace('it is always, ok','not always')
Upvotes: 1