| | |
Help with string formatting
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2009
Posts: 73
Reputation:
Solved Threads: 0
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 31 Days Ago
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 31 Days Ago
•
•
•
•
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.
•
•
Join Date: Nov 2009
Posts: 73
Reputation:
Solved Threads: 0
0
#6 31 Days Ago
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.
•
•
Join Date: Nov 2009
Posts: 73
Reputation:
Solved Threads: 0
0
#7 31 Days Ago
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 31 Days Ago
•
•
•
•
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; 31 Days Ago 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
| Thread Tools | Search this Thread |
abrupt ansi anti approximation array assignment avogadro backend beginner binary bluetooth builtin calculator character chmod code converter countpasswordentry curved customdialog dan08 decimals dictionaries dictionary drive dynamic examples exe file float format function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse mysqlquery number numbers output parsing path plugin pointer port prime programming progressbar projects py2exe pygame pyqt pysimplewizard python random recursion scrolledtext sqlite statistics stdout string strings sum table terminal text textarea thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable windows write wxpython xlib





