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!

You can use regular expressions and the re.sub function, like this

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.
"""

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.

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

If it's not too much trouble, would you mind explaining step-by-step what the code does please?

The statement

special = re.compile(r"[etn]")

creates a regular expression object, which represents the set of characters 'e', 't' or 'n'. When

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.

Wow!

That explains so much, thanks! I've written my own snippet, much like yours:

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.

Wow!

That explains so much, thanks! I've written my own snippet, much like yours:

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.

Wow!

That explains so much, thanks! I've written my own snippet, much like yours:

foo = open("PREFIX.txt", "r+")
text = foo.read

You must CALL the read method to get the file's content

text = foo.read()

Also please mark your other thread with the same error as solved.

You are my Python hero!

Thanks a lot. Slapped myself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.