| | |
Help with string formatting
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2009
Posts: 70
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 28 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 28 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: 70
Reputation:
Solved Threads: 0
0
#6 28 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: 70
Reputation:
Solved Threads: 0
0
#7 28 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 28 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; 28 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 assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog data decimals dictionaries dictionary directory drive dynamic error examples exe file float format function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule script scrolledtext sqlite statistics stdout string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia windows write wxpython xlib





