I am trying to split a string in python into a list of characters. I know that there are a lot of ways to do this in python, but I have a case where those methods don't give me the desired results.
The problem happens when I have special characters like '\t' that is explicitly written in the string (and I don't mean the real tab).
Example:
string = " Hello \t World."
the output I need is:
list_of_chars = [' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '\', 't', ' ', 'W', 'o', 'r', 'l', 'd', '.']
but when I use the methods that are given in this question, I get a list that contains '/t' as whole string - not separated.
Example:
> list(string)
> ['H', 'e', 'l', 'l', 'o', 'w', ' ', '\t', ' ', 'W', 'o', 'r', 'l', 'd', '.']
I want to know why this happens and how to get what I want.
You can substitute your string accordingly:
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))
Output:
[' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '\\', 't', ' ', 'W',
'o', 'r', 'l', 'd', '.']
The list comprehension looks more "positive" and uses list(itertools.chain(*[...]))
instead of list("".join([...]))
which should be more performant.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With