> > 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?
>
Guido.van.Rossum@cwi.nl answered:
> Where's your perseverence :-) You should've quadrupled each backslash...
Or, alternatively, you can sometimes use 'repr' ( or backquotes ) to
avoid messing with those escapes ( however, note that you get functionally,
but not quite *literally*, the same thing: '\n' is transformed to '\012'):
>>> print "echo 'This is a \n test!\n'"
echo 'This is a
test!
'
>>> print repr("echo 'This is a \n test!\n'" )
"echo 'This is a \012 test!\012'"
# we need to strip off the outer quotes:
>>> print repr("echo 'This is a \n test!\n'" )[1:-1]
echo 'This is a \012 test!\012'
# and let's see what a shell does with that line:
>>> import os
>>> os.system( repr("echo 'This is a \n test!\n'" )[1:-1] )
This is a
test!
0
---| Steven D. Majewski (804-982-0831) <sdm7g@Virginia.EDU> |---
---| Computer Systems Engineer University of Virginia |---
---| Department of Molecular Physiology and Biological Physics |---
---| Box 449 Health Science Center Charlottesville,VA 22908 |---