How to split with " \\" character in a list

Viewed 39

So currently I have this list in python :

list1= [ '\\[192.168.1.2] [Entered 123456] successful!' ,  '[192\\.168.1.1] [Entered 157] unwilling operator! ', '[192\\.168.1.3] [Entered 56]  not successful' ]

I only need the list elements after the "\" but when I tried

    for i in list1:
    res = i.split("\\")[:2]
    print(res)

The output only shows blank elements

1 Answers

"\\" is a string consisting of a single backslash; the backslash itself has to be escaped to be treated literally.

Add an r flag in your search string to define a raw string literal, in which backslashes are treated literally.

res = i.split(r"\\")[:2]

Related