How to split "\ t" in a string into two separate characters like "\" and "t"? (How to split Escape Sequence?)

I am trying to split a string in python into a list of characters. I know that in python there are many ways to do this, but I have a case where these methods do not give me the desired results.

The problem arises when I have special characters, such as '\ t', that are explicitly written in the string (and I don't mean the real tab).

Example:

string = "    Hello \t World."

I need the result:

list_of_chars = [' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '\', 't', ' ', 'W', 'o', 'r', 'l', 'd', '.']

but when I use the methods given in this question , I get a list that contains '/ t' as a whole line - not split.

Example:

> list(string)
> ['H', 'e', 'l', 'l', 'o', 'w', ' ', '\t', ' ', 'W', 'o', 'r', 'l', 'd', '.']

I want to know why this is happening and how to get what I want.

+4
source share
4

:

import itertools
txt = "    Hello \t World."

specials = { 
    '\a' : '\\a', #     ASCII Bell (BEL)
    '\b' : '\\b', #     ASCII Backspace (BS)
    '\f' : '\\f', #     ASCII Formfeed (FF)
    '\n' : '\\n', #     ASCII Linefeed (LF)
    '\r' : '\\r', #     ASCII Carriage Return (CR)
    '\t' : '\\t', #     ASCII Horizontal Tab (TAB)
    '\v' : '\\v'  #     ASCII Vertical Tab (VT)
}

# edited out: # txt2 = "".join([x if x not in specials else specials[x] for x in txt])
txt2 = itertools.chain(* [(list(specials[x]) if x in specials else [x]) for x in txt])

print(list(txt2))

:

[' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '\\', 't', ' ', 'W', 
 'o', 'r', 'l', 'd', '.'] 

"" list(itertools.chain(*[...])) list("".join([...])), .

+6

String Literal, :

(\) , , , , . r' or R '; escape- .

\t , , ASCII (TAB).

Python, , ( r "") :

>>> list(r"    Hello \t World.")
[' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '\\', 't', ' ', 'W', 'o', 'r', 'l', 'd', '.']

\\ , Python \.

Python '\' , \' ('). , '\', , Python :

>>> '\'
  File "<stdin>", line 1
    '\'
      ^
SyntaxError: EOL while scanning string literal

( - ), , "unicode-escape":

>>> my_str = "    Hello \t World."

>>> unicode_escaped_string = my_str.encode('unicode-escape')
>>> unicode_escaped_string
b'    Hello \\t World.'

, chr, . :

>>> list(map(chr, unicode_escaped_string))
[' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '\\', 't', ' ', 'W', 'o', 'r', 'l', 'd', '.']
+5

, Python, ?

string = "    Hello \t World."
string_raw = string.encode('unicode-escape')
print([ch for ch in string_raw])
print([chr(ch) for ch in string_raw])

:

[32, 32, 32, 32, 72, 101, 108, 108, 111, 32, 92, 116, 32, 87, 111, 114, 108, 100, 46]
[' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '\\', 't', ' ', 'W', 'o', 'r', 'l', 'd', '.']

Ascii 92 - , , , , .

+4

\t , \, :

string = "    Hello \\t World."

:

string = r"    Hello \t World."
-1

Source: https://habr.com/ru/post/1691593/


All Articles