7

I want to write "\t" to a file. Not a "tab character", but literally "\t". How do I do this? It has been driving me crazy for more than an hour; everything I try produces an actual "tab character" (empty space).

Why would I want this? Because I'm generating a config file with a ConfigParser() which also includes some delimiters, and I want that file to be human-readable. I do not consider empty space readable.

EDIT: sorry, the problem was not clear: I want to do this for variables that contain strings. So writing "\\t" is not an option. I must write the value of a variable containing an escaped character to a file in a manner ideologically equivalent to:

v = "\t"
write(v)

without changing the definition of v (though operations on v are OK) It seems impossible to change v into "\\t" after it has been defined as "\t".

PDiracDelta
  • 2,348
  • 5
  • 21
  • 43

5 Answers5

5

You need to escape the backslash so it won't be interpreted as a special character:

file_handle.write("\\t")

EDIT: It looks like your best option will be to use python's excellent string.replace() function:

>>> v = "aaa \t bbb"
>>> print v
aaa      bbb
>>> x = v.replace("\t", "\\t")  # <-- replace all '\t' in string with '\\t'
>>> print x
aaa \t bbb

Note: v will be unchanged, since strings are immutable in python

EDIT 2:

Looks like converting to a raw string will take care of escapeing all escaped characters for you:

>>> a = 'aaa\nbbb\n\tccc'
>>> print a
aaa
bbb
        ccc
>>> b = a.encode('string-escape')
>>> print b
aaa\nbbb\n\tccc
Zoe
  • 27,060
  • 21
  • 118
  • 148
Will
  • 4,299
  • 5
  • 32
  • 50
  • sorry, I edited the question so my problem is more clear – PDiracDelta Dec 09 '16 at 15:43
  • @PDiracDelta edited, i think this will be what you need. I'm using 2.7, but it should be the same as 3 AFAIK – Will Dec 09 '16 at 16:28
  • thanks for the hint, although this is not a general solution for any escape char - I still need to hardcode each possible escaped character. But for my practical purposes, it will do :-) – PDiracDelta Dec 09 '16 at 20:46
  • @PDiracDelta I can't seem to get it to work for `v.replace('\\', '\\\\')`. Asked a new question here: http://stackoverflow.com/questions/41068918/cant-escape-escape-characters-in-string – Will Dec 09 '16 at 21:09
  • also see here: http://stackoverflow.com/questions/2428117/casting-raw-strings-python – Will Dec 09 '16 at 21:35
1
>>> repr('\t')
"'\\t'"

https://docs.python.org/3/library/functions.html#repr

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
0

Escape it.

Instead of writing \t write \\t

Christian W.
  • 2,532
  • 1
  • 19
  • 31
0

Lets assume v is 'foo'. You need the escape your single backslash.

v = '\\t %s' % v
print(v)

returns:

\t foo
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
0

I've found a decoder workaround!

from codecs import getencoder as ge, getdecoder as gd
v = "\t"
ge("unicode-escape")(v)[0]
v = gd("utf-8")(byteDelim)[0]
write(v)

using the encoder one can introduce the extra "\" to make a tuple (b'\\t', 1). Then deconstruct the byte-like string in the tuple using another decoder that does not remove the "\", like utf-8.

PDiracDelta
  • 2,348
  • 5
  • 21
  • 43