| | |
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: 266 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for Python
address anydbm app backend bash beginner bits calling class code conversion coordinates copy curves dictionary directory dynamic edit examples excel feet file float format ftp function generator gui halp homework http i/o images import info input ip itunes java keycontrol line linux list lists loop maintain millimeter mouse newb number numbers output panel parsing path port prime print program programming projects py-mailer py2exe pygame pyqt python queue random rational recursion recursive scrolledtext server smtp split ssh statictext string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode update urllib urllib2 variable whileloop windows write wxpython





