String replacements

Glen Collins (glenc@netcom.com)
Wed, 29 Mar 1995 17:54:12 GMT

Hello Python gurus!

I have a question!

I am using python in a UNIX environment to read in shell scripts and
modify them using python. Some of the file contain echo statments which
have to include "\n" in them. What I would like to be able to do is
read them in and write them back out. But python seems to change these
into their proper value when written. In other words I get a carriage
return in the actual resulting file and not a "\n". Sample:

tmpstr="echo 'this is a test\nhello world\n'"

will look like

tmpstr="echo 'this is a test
hello world
'"

Now what I seem to have to do is change the "\n" into "\\n" in order
for the resulting file to be written as sampled above. I can use a
sed script to do this. sample:

s/\\n/\\\\n/g

This will work, but I want to use python. Now I have tried the following:

regsub.gsub('\n','\\n',<var>)

Will this even work? I have even tried to double and tripple the "\" but
it does not seem to work. Any ideas?