944,120 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 455
  • Python RSS
Nov 4th, 2009
0

Help with string formatting

Expand Post »
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!
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster
P00dle is offline Offline
154 posts
since Nov 2009
Nov 4th, 2009
0
Re: Help with string formatting
You can use regular expressions and the re.sub function, like this
python Syntax (Toggle Plain Text)
  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. """
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,656 posts
since Jul 2008
Nov 4th, 2009
0
Re: Help with string formatting
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.
Reputation Points: 10
Solved Threads: 1
Junior Poster
P00dle is offline Offline
154 posts
since Nov 2009
Nov 4th, 2009
0
Re: Help with string formatting
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
Reputation Points: 10
Solved Threads: 1
Junior Poster
P00dle is offline Offline
154 posts
since Nov 2009
Nov 4th, 2009
0
Re: Help with string formatting
Click to Expand / Collapse  Quote originally posted by P00dle ...
If it's not too much trouble, would you mind explaining step-by-step what the code does please?
The statement
python Syntax (Toggle Plain Text)
  1. special = re.compile(r"[etn]")
creates a regular expression object, which represents the set of characters 'e', 't' or 'n'. When
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,656 posts
since Jul 2008
Nov 4th, 2009
0
Re: Help with string formatting
Wow!

That explains so much, thanks! I've written my own snippet, much like yours:
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Junior Poster
P00dle is offline Offline
154 posts
since Nov 2009
Nov 4th, 2009
0
Re: Help with string formatting
Wow!

That explains so much, thanks! I've written my own snippet, much like yours:
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Junior Poster
P00dle is offline Offline
154 posts
since Nov 2009
Nov 4th, 2009
0
Re: Help with string formatting
Click to Expand / Collapse  Quote originally posted by P00dle ...
Wow!

That explains so much, thanks! I've written my own snippet, much like yours:
Python Syntax (Toggle Plain Text)
  1. foo = open("PREFIX.txt", "r+")
  2. text = foo.read
You must CALL the read method to get the file's content
python Syntax (Toggle Plain Text)
  1. text = foo.read()
Also please mark your other thread with the same error as solved.
Last edited by Gribouillis; Nov 4th, 2009 at 5:16 am.
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,656 posts
since Jul 2008
Nov 4th, 2009
0
Re: Help with string formatting
You are my Python hero!

Thanks a lot. Slapped myself.
Reputation Points: 10
Solved Threads: 1
Junior Poster
P00dle is offline Offline
154 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Help with Pascal Triangle Program Please
Next Thread in Python Forum Timeline: Array Index Issue





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC