16

I have a quick question. I am trying to split a string S : 'greenland.gdb\topology_check\t_buildings' at '\' using:

     S.split('\')

I expect output list :

 ['greenland.gdb', 'topology_check', 't_buildings']. 

Instead it returns error : SyntaxError: EOL while scanning string literal. What is with character '\' in python. With any other character it works fine.

4 Answers 4

21

You need to escape the backslash:

 S.split('\\')

You may also need to string_escape:

In [10]: s = 'greenland.gdb\topology_check\t_buildings'

In [11]: s.split("\\")
Out[11]: ['greenland.gdb\topology_check\t_buildings']

In [12]: s.encode("string_escape").split("\\")
Out[12]: ['greenland.gdb', 'topology_check', 't_buildings']

\t would be interpreted as a tab character unless you were using a raw string:

In [18]: s = 'greenland.gdb\topology_check\t_buildings'

In [19]: print(s)
greenland.gdb   opology_check   _buildings

In [20]: s = r'greenland.gdb\topology_check\t_buildings'

In [21]: print(s)
greenland.gdb\topology_check\t_buildings

Escape characters

Sign up to request clarification or add additional context in comments.

5 Comments

it doesnot work . i have already tried. '\\' is a different character in itself.
@Larry, no it h is not a different characer, if you want a literal `\` then you need to escape it
that makes sense. it works with methods encode and split in cascade . Thanks !
This doesn't work anymore for python3 (LookupError: unknown encoding: string_escape). Any idea?
I just found it should be unicode_escape in python3, but it is not suitable for string. only works for byte-like data
3

You need to use a raw string first, 'r' or 'R' as follows:

In [01]: s = r'greenland.gdb\topology_check\t_buildings'

Then split while remembering to escape the backslash

In [02]:  s.split('\\')

Out [02]: ['greenland.gdb', 'topology_check', 't_buildings']

Quoting from re — Regular expression operations (Python documentation)

\ Either escapes special characters (permitting you to match characters like '*', '?', and so forth), or signals a special sequence; special sequences are discussed below.

If you’re not using a raw string to express the pattern, remember that Python also uses the backslash as an escape sequence in string literals; if the escape sequence isn’t recognized by Python’s parser, the backslash and subsequent character are included in the resulting string. However, if Python would recognize the resulting sequence, the backslash should be repeated twice. This is complicated and hard to understand, so it’s highly recommended that you use raw strings for all but the simplest expressions.

1 Comment

Awesome! This address the issue mentioned by this comment with Python3
3

Backslash \ is a special character , I dont like magic too but ,
in this case (and for both python 2.7 and python 3 ):

>>> s = 'greenland.gdb\topology_check\t_buildings'
>>> s.split()

will give the desired output :

['greenland.gdb', 'opology_check', '_buildings']

Comments

-1

s = 'greenland.gdb**\topology_check\t**_buildings' python is consider this \t has the escape sequence . \t - Indicates the whitespace in python.

for the above case we won't specify any separater .space considered as default separater. input- print (s.split()) output- ['greenland.gdb', 'opology_check', '_buildings']

If I am wrong correct me .

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.