Raw String Output
Aahz
aahz at pythoncraft.com
Thu Jun 20 20:20:16 EDT 2002
In article <Xns9234F35131B7cliechtigmxnet at 62.2.16.82>,
Chris Liechti <cliechti at gmx.net> wrote:
>"mike" <gringro1 at umbc.edu> wrote in news:aetnkf$tot$1 at news.umbc.edu:
>>
>> If I have a string variable with the value '\n' how do I output it to
>> a file with the value of '\012' instead ?
>
>i assume that its clear to you that '\n' == '\012' so i guess you want the
>string r'\012' in the file and not the newline character?!?
>
>then you must do it by hand, i think. this one replaces all characters < 32
>with their octal value:
>>>> print ''.join([ c>=' ' and c or '\\%03o' % ord(c) for c in 'hello\n'])
>hello\012
Actually, I'd guess the problem is more that Mike is using a text file
on a platform where \n is *not* \012 (e.g. Windows). In that case,
he'll have to stick chr(012) into the files directly, preferably opening
files in binary mode first.
--
Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/
Project Vote Smart: http://www.vote-smart.org/
More information about the Python-list
mailing list