On 7/5/17, Binary Boy <blahb...@blah.org> wrote: > On Wed, 05 Jul 2017 20:37:38 +0300, Jussi Piitulainen wrote: >> Sam Chats writes: >> >> > On Wednesday, July 5, 2017 at 9:09:18 PM UTC+5:30, Grant Edwards wrote: >> >> On 2017-07-05, Sam Chats <blahb...@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: >> >> [...] >> >> > How can I fix this? >> >> >> >> That depends on what you mean by "as-is". >> >> >> >> Seriously. >> >> >> >> Do you want the single quotes in the file? Do you want the backslash >> >> and 't' character in the file? >> >> >> >> When you post a question like this it helps immensely to provide an >> >> example of the output you desire. >> > >> > I would add to add the following couple lines to a file: >> > >> > for i in range(5): >> > print('Hello\tWorld') >> > >> > Consider the leading whitespace to be a tab. >> >> import sys >> >> lines = r''' >> for line in range(5): >> print('hello\tworld') >> ''' >> >> print(lines.strip()) >> >> sys.stdout.write(lines.strip()) >> sys.stdout.write('\n') > > Thanks! But will this work if I already have a string through a string > variable, rather than using it directly linke you did (by declaring the > lines variable)? > And, will this work while writing to files? > > Sam
If I understand you well then no. >>> a = '%s' % 'a\tb' # we have string with tab (similar as we expect from >>> NNTP server?) >>> print(a) # this is not what you like to have in file a b >>> print(repr(a)) # maybe this is conversion you need 'a\tb' >>> print(repr(a)[1:-1]) # or maybe this a\tb -- https://mail.python.org/mailman/listinfo/python-list