1

Possible Duplicate:
Python: Split string with multiple delimiters
Convert django Charfield “\t” to tab

I have written a python code in Eclipse which takes delimiters as an argument. When I do

print "Hello",delimiter, "All".

This generates --> Hello \t All, whereas if I overwrite the delimiter with delimiter = '\t' within the code, I get the right output Hello All. I wonder what is the difference? I hope this not just the eclipse thing.

Community
  • 1
  • 1
mrig
  • 382
  • 1
  • 4
  • 21
  • Can you do `print len(delimiter)` for both the one passed in from the command line and the one you overwrite in code – Matti Lyra Nov 17 '12 at 08:45
  • 1
    Good stuff. The problem is that `\t` as fed in from the command line is treated as a string of two characters, `print delimiter` should print `'\\t'`, where if you wanted it to create a tab space it should be a string of one character, thus `len('\t')` print `1` for the second case. – Matti Lyra Nov 17 '12 at 09:07

1 Answers1

3

The problem is that what is being passed in from the command line is actually a string of length two "\\t" and not a tab character. You can do the following to your delimiter

delimiter.decode("string_escape"))

that should convert the string '\\t' into '\t'. The answer comes from a duplicate questions here

Community
  • 1
  • 1
Matti Lyra
  • 12,828
  • 8
  • 49
  • 67