| | |
Help with string formatting
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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!
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!
0
#2 Nov 4th, 2009
You can use regular expressions and the re.sub function, like this
python Syntax (Toggle Plain Text)
import re special = re.compile(r"[etn]") def escape(match): return "\\" + match.group(0) if __name__ == "__main__": text = "This is a test sentence." print text print special.sub(escape, text) r""" my output ---> This is a test sentence. This is a \t\es\t s\e\n\t\e\nc\e. """
0
#5 Nov 4th, 2009
•
•
•
•
If it's not too much trouble, would you mind explaining step-by-step what the code does please?
python Syntax (Toggle Plain Text)
special = re.compile(r"[etn]")
python Syntax (Toggle Plain Text)
special.sub(escape, text)
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.
0
#6 Nov 4th, 2009
Wow!
That explains so much, thanks! I've written my own snippet, much like yours:
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.
That explains so much, thanks! I've written my own snippet, much like yours:
Python Syntax (Toggle Plain Text)
import re foo = open("PREFIX.txt", "r+") text = foo.read foo.close() special = re.compile(r"\\") def escape(match): return "\\" + match.group(0) if __name__ == "__main__": out = open("WRITE.txt", "w") out.write(str(text)) out.write(special.sub(escape, text)) 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.
0
#7 Nov 4th, 2009
Wow!
That explains so much, thanks! I've written my own snippet, much like yours:
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.
That explains so much, thanks! I've written my own snippet, much like yours:
Python Syntax (Toggle Plain Text)
import re foo = open("PREFIX.txt", "r+") text = foo.read foo.close() special = re.compile(r"\\") def escape(match): return "\\" + match.group(0) if __name__ == "__main__": out = open("WRITE.txt", "w") out.write(str(text)) out.write(special.sub(escape, text)) 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.
0
#8 Nov 4th, 2009
•
•
•
•
Wow!
That explains so much, thanks! I've written my own snippet, much like yours:
Python Syntax (Toggle Plain Text)
foo = open("PREFIX.txt", "r+") text = foo.read
python Syntax (Toggle Plain Text)
text = foo.read()
Last edited by Gribouillis; Nov 4th, 2009 at 5:16 am.
![]() |
Similar Threads
- Code Snippet: string formatting specifications (Python)
- String formatting (C#)
- VC++ bloody problem. What to use for string formatting (C++)
- String formatting using the "\t" character (Java)
- Date formatting? (Pascal and Delphi)
Other Threads in the Python Forum
- Previous Thread: Help with Pascal Triangle Program Please
- Next Thread: Array Index Issue
Views: 279 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for Python
approximation array beginner book builtin change cipher clear client code color converter countpasswordentry cturtle curved def dictionary drive dynamic examples excel file float format ftp function gui homework import inches input java library line lines linux list lists loop microcontroller mouse mysqldb mysqlquery newb number numbers output parsing path plugin port prime program programming projects py2exe pygame pymailer pyqt python random recursion recursive redirect remote script scrolledtext search singleton socket sqlite ssh string strings strip subprocess sum syntax table terminal text textarea thread threading time tkinter tlapse tuple tutorial twoup ubuntu unicode urllib urllib2 variable vigenere wikipedia windows wxpython xlwt





