osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to write raw strings to Python


On Wednesday, July 5, 2017 at 8:22:13 PM UTC+5:30, Stephen Tucker wrote:
> Sam,
> 
> You use
> 
> f.write(r'hello\tworld')
> 
> The r in front of the string stands for raw and is intended to switch off
> the escape function of the backslash in the string. It works fine so long
> as the string doesn't end with a backslash, as in
> 
> f.write('hello\tworld\')
> 
> If you try this, you get an error message.
> 
> Stephen.
> 
> 
> 
> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
> Virus-free.
> www.avast.com
> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
> <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
> 
> On Wed, Jul 5, 2017 at 3:37 PM, Sam Chats <blahBlah at blah.org> wrote:
> 
> > I want to write, say, 'hello\tworld' as-is to a file, but doing
> > f.write('hello\tworld') makes the file
> > look like:
> > hello   world
> >
> > How can I fix this? Thanks in advance.
> >
> > Sam
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >

Thanks, but I've tried something similar. Actually, I want to convert a string which I receive from a NNTP server to a raw string. So if I try something like:
raw = r"%s" % string_from_server

It doesn't work.

Regards,
Sam