5

I want to receive a delimiter like '\t' (tab) from command line, and use it to parse a text file.

If I put

delimiter = sys.argv[1]

in the code, and type from the command line

$ python mycode.py "\t"

delimiter is '\\t' i.e., python does its thing to preserve input string as is.

I want to convert this to '\t' so that I can use e.g.,

'a\tb\tc'.split(delimiter)

to get ['a','b','c'].

I've tried to convert '\' to '\', but failed.

Is there a built-in python function to handle regex from the command line?

1
  • 1
    why not use re.split()? Commented Oct 20, 2012 at 14:13

2 Answers 2

4

In Python 2 you can use str.decode('string_escape'):

>>> '\\t'.decode('string_escape')
'\t'

In Python 3 you have to encode the string to bytes first and then use unicode_escape:

>>> '\\t'.encode().decode('unicode_escape')
'\t'

Both solutions accept any escape sequence and will decode them correctly, so you could even use some fancy unicode stuff:

>>> '\\t\\n\\u2665'.encode().decode('unicode_escape')
'\t\n♥'
Sign up to request clarification or add additional context in comments.

1 Comment

I think this is the simplest solution to my problem. I see I was searching the wrong keyword. Thanks everybody!
1

It's not really regexp you're looking for, it's escape sequences.

You could use eval, as long as you're perfectly aware of the terrible security consequences, or roll your own string replacement/regexp based escape sequence unescaper.

(Who knows, maybe arg = arg.replace("\\t", "\t") is enough for you?)

As a workaround you could do

$ python mycode.py `echo -ne '\t'`

to (ab) use the Unix echo command to do the unescaping for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.