7 Replies - 555 Views - Last Post: 18 May 2012 - 10:28 AM Rate Topic: -----

#1 trigger202  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-March 11

reading file and writing to files plz help

Posted 18 May 2012 - 07:49 AM

hi guys

i want to change the text in the file for example
change all the a's to e
all the e's to a
but my problem is when i do the first step all the a's are changed to e and then later changed back to all a's

the test.txt file contains("this is a message )

so i want to make the changes once
and the result should be (""thi3 i3 e ma33ega"
i hope that the explanation was clear
plz help
def makeChangesToFile():

  infile=open("test.txt","r")
  contents=infile.read()
  infile.close()
  tofile=open("newContents.txt","w")
  
  
  contents=contents.replace("a","e")
 
  contents=contents.replace("e","a")
  contents=contents.replace("s","3")
  
  tofile.write(contents)
  tofile.close()
makeChangesToFile()




Is This A Good Question/Topic? 0
  • +

Replies To: reading file and writing to files plz help

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5672
  • View blog
  • Posts: 22,526
  • Joined: 23-August 08

Re: reading file and writing to files plz help

Posted 18 May 2012 - 07:53 AM

And the problem is WHAT exactly?
Was This Post Helpful? 0
  • +
  • -

#3 trigger202  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-March 11

Re: reading file and writing to files plz help

Posted 18 May 2012 - 07:59 AM

the problem is
the contents changes to

in this line contents=contents.replace("a","e") >= "thi3 i e messege"
and contents=contents.replace("e","a") t>= thi3 i3 a massaga

so it needs to be done at once otherwise each statement is going to modify the content again
this should be the result "thi3 i3 e ma33ega"
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4888
  • View blog
  • Posts: 11,284
  • Joined: 16-October 07

Re: reading file and writing to files plz help

Posted 18 May 2012 - 08:31 AM

You already have your answer, then. You can't use the canned replace function. You'll have to do it yourself.

Loop through each character in the string, compare it to your replacement list, and replace a required.
Was This Post Helpful? 0
  • +
  • -

#5 trigger202  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-March 11

Re: reading file and writing to files plz help

Posted 18 May 2012 - 08:36 AM

View Postbaavgai, on 18 May 2012 - 08:31 AM, said:

You already have your answer, then. You can't use the canned replace function. You'll have to do it yourself.

Loop through each character in the string, compare it to your replacement list, and replace a required.



 ok but i still  dont know how to replace the letter to another without using  the replace funtion can you give me an example or something please 


Was This Post Helpful? 0
  • +
  • -

#6 trigger202  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-March 11

Re: reading file and writing to files plz help

Posted 18 May 2012 - 10:01 AM

View Posttrigger202, on 18 May 2012 - 08:36 AM, said:

View Postbaavgai, on 18 May 2012 - 08:31 AM, said:

You already have your answer, then. You can't use the canned replace function. You'll have to do it yourself.

Loop through each character in the string, compare it to your replacement list, and replace a required.



 ok but i still  dont know how to replace the letter to another without using  the replace funtion can you give me an example or something please 




am confused now i tried different ways i cant get the hang of it
i want to change/replace a letter which is in the text file already with another then write that to a file

what am i doing wrong
the aim is to look for a letter if it exists change it with another
then write the newly modified content into a file

def makeChangesToFile():

  infile=open("test.txt","r")
  contents=infile.read()
  infile.close()
  tofile=open("newContents.txt","w")
  
  for letter in contents:
    if letter=="a":
      letter="e"
      letter=contents+letter
      
     
      
    elif letter=="e":
        letter="a"
        contents=contents+letter
   
  printNow(contents)
  tofile.write(contents)
  
 
  tofile.close()
makeChangesToFile()




Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4888
  • View blog
  • Posts: 11,284
  • Joined: 16-October 07

Re: reading file and writing to files plz help

Posted 18 May 2012 - 10:19 AM

for letter in contents: # fine
	if letter=="a": # fine
		letter="e" # not sure, won't kill you, but not great.
		letter=contents+letter # WTF?  What is this supposed to mean?



Think about what you're trying to do.
newContents = "" # start with an empty string
for letter in contents: # loop through all the characters of the source string
	if letter=="a": # if you find a letter you want to replace
		newContents += "e" # replace it
	# more code here
	else: # if there are no replacements, use the original letter
		newContents += letter


Was This Post Helpful? 0
  • +
  • -

#8 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 732
  • View blog
  • Posts: 1,880
  • Joined: 23-December 08

Re: reading file and writing to files plz help

Posted 18 May 2012 - 10:28 AM

You need to tell us what your problem is if this approach isn't working for you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1