Help with string formatting

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2009
Posts: 70
Reputation: P00dle is an unknown quantity at this point 
Solved Threads: 0
P00dle P00dle is offline Offline
Junior Poster in Training

Help with string formatting

 
0
  #1
28 Days Ago
Hi,

I need to read input from 2 different files, take text from both, combine it, then print it to a third file. That part is easy. What I have a problem with is that some of the text I read in have special characters that need to be escaped.

How can I handle those characters?

Any help would be appreciated, but I would obviously prefer a snippet of code I could use.

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 930
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 216
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark
 
0
  #2
28 Days Ago
You can use regular expressions and the re.sub function, like this
  1. import re
  2.  
  3. special = re.compile(r"[etn]")
  4.  
  5. def escape(match):
  6. return "\\" + match.group(0)
  7.  
  8. if __name__ == "__main__":
  9. text = "This is a test sentence."
  10. print text
  11. print special.sub(escape, text)
  12.  
  13. r""" my output --->
  14. This is a test sentence.
  15. This is a \t\es\t s\e\n\t\e\nc\e.
  16. """
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 70
Reputation: P00dle is an unknown quantity at this point 
Solved Threads: 0
P00dle P00dle is offline Offline
Junior Poster in Training
 
0
  #3
28 Days Ago
Originally Posted by Gribouillis View Post
You can use regular expressions and the re.sub function
Thanks!

I am going to try it now, and will let you know if it has worked, or not.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 70
Reputation: P00dle is an unknown quantity at this point 
Solved Threads: 0
P00dle P00dle is offline Offline
Junior Poster in Training
 
0
  #4
28 Days Ago
The code you supplied works just fine on its own. I need to be able to use it in a different situation.

If it's not too much trouble, would you mind explaining step-by-step what the code does please?

Thank you
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 930
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 216
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark
 
0
  #5
28 Days Ago
Originally Posted by P00dle View Post
If it's not too much trouble, would you mind explaining step-by-step what the code does please?
The statement
  1. special = re.compile(r"[etn]")
creates a regular expression object, which represents the set of characters 'e', 't' or 'n'. When
  1. special.sub(escape, text)
is executed, all the occurrences of these characters are matched in the text and replaced by the output of the function 'escape'. A new string is returned.
The function 'escape' takes a 'match object' as argument which represents a match of the regex in a string and returns the text of the match preceded by a backslash.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 70
Reputation: P00dle is an unknown quantity at this point 
Solved Threads: 0
P00dle P00dle is offline Offline
Junior Poster in Training
 
0
  #6
28 Days Ago
Wow!

That explains so much, thanks! I've written my own snippet, much like yours:
  1. import re
  2.  
  3. foo = open("PREFIX.txt", "r+")
  4. text = foo.read
  5. foo.close()
  6.  
  7. special = re.compile(r"\\")
  8.  
  9. def escape(match):
  10. return "\\" + match.group(0)
  11.  
  12. if __name__ == "__main__":
  13. out = open("WRITE.txt", "w")
  14. out.write(str(text))
  15. out.write(special.sub(escape, text))
  16. out.close()

The following error appears, however(I am running it via the command console):
TraceBack (most recent call last):
File "advanced,py", line 15, in (module)
out.writeout.write(str(special.sub(escape, text)))
TypeError: expected string or buffer

I've also tried to use it run it while line 15 looked like this: out.write(special.sub(escape, text))

I have only started using Python yesterday, but the company needs me to use it now, for this, so please bear that in mind...

Thank you for the help, hopefully you can assist withthis as well.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 70
Reputation: P00dle is an unknown quantity at this point 
Solved Threads: 0
P00dle P00dle is offline Offline
Junior Poster in Training
 
0
  #7
28 Days Ago
Wow!

That explains so much, thanks! I've written my own snippet, much like yours:
  1. import re
  2.  
  3. foo = open("PREFIX.txt", "r+")
  4. text = foo.read
  5. foo.close()
  6.  
  7. special = re.compile(r"\\")
  8.  
  9. def escape(match):
  10. return "\\" + match.group(0)
  11.  
  12. if __name__ == "__main__":
  13. out = open("WRITE.txt", "w")
  14. out.write(str(text))
  15. out.write(special.sub(escape, text))
  16. out.close()

The following error appears, however(I am running it via the command console):
TraceBack (most recent call last):
File "advanced,py", line 15, in (module)
out.writeout.write(str(special.sub(escape, text)))
TypeError: expected string or buffer

I've also tried to use it run it while line 15 looked like this: out.write(special.sub(escape, text))

I have only started using Python yesterday, but the company needs me to use it now, for this, so please bear that in mind...

Thank you for the help, hopefully you can assist withthis as well.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 930
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 216
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark
 
0
  #8
28 Days Ago
Originally Posted by P00dle View Post
Wow!

That explains so much, thanks! I've written my own snippet, much like yours:
  1. foo = open("PREFIX.txt", "r+")
  2. text = foo.read
You must CALL the read method to get the file's content
  1. text = foo.read()
Also please mark your other thread with the same error as solved.
Last edited by Gribouillis; 28 Days Ago at 5:16 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 70
Reputation: P00dle is an unknown quantity at this point 
Solved Threads: 0
P00dle P00dle is offline Offline
Junior Poster in Training
 
0
  #9
28 Days Ago
You are my Python hero!

Thanks a lot. Slapped myself.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC