I want to remove all types of escape sequences from a list of strings. How can I do this? input: Show
output:
http://docs.python.org/reference/lexical_analysis.html#string-literals asked Nov 13, 2011 at 22:28
1 If you want to strip out some characters you don't like, you can use the translate function to strip them out:
This will strip out all these control characters:
For Python newer than 3.1, the sequence is different:
answered Nov 13, 2011 at 22:45
sarnoldsarnold 100k21 gold badges177 silver badges229 bronze badges 5 Something like this?
Edit: ok, that's not what you want. What you want can't be done in general, because, as @Sven Marnach explained, strings don't actually contain escape sequences. Those are just notation in string literals. You can filter all strings with non-ASCII characters from your list with
answered Nov 13, 2011 at 22:32
Fred FooFred Foo 347k72 gold badges724 silver badges824 bronze badges You could filter out "words" that are not alphanumeric using a list comprehension and
If you wish to filter out numbers, too, use
answered Nov 13, 2011 at 22:34
johnsywebjohnsyweb 132k23 gold badges178 silver badges239 bronze badges 1 This cannot be done, at least at the broad scope you are asking. As others have mentioned, runtime python doesn't know the difference between the something with escape sequences, and something without. Example:
prints answered Nov 13, 2011 at 22:45
Adam WagnerAdam Wagner 14.8k7 gold badges52 silver badges65 bronze badges The following worked for me in Python 3.6:
Output:
Source here answered Sep 18, 2019 at 5:06
JerilJeril 7,0553 gold badges50 silver badges66 bronze badges 1 I had similar issues while converting from hexadimal to String.This is what finally worked in python Example
royhowie 10.8k14 gold badges49 silver badges67 bronze badges answered Sep 13, 2013 at 6:45
AKVAKV 1711 gold badge4 silver badges12 bronze badges 0 How do I remove an escape character from a string in Python?Practical Data Science using Python
You can use regexes to remove the ANSI escape sequences from a string in Python. Simply substitute the escape sequences with an empty string using re. sub(). The regex you can use for removing ANSI escape sequences is: '(\x9B|\x1B\[)[0-?]
How do you remove quotes from a string in Python?Use str.
strip(chars) on str with the quote character '"' as chars to remove quotes from the ends of the string.
How do you remove quotes from a string?To remove double quotes just from the beginning and end of the String, we can use a more specific regular expression: String result = input. replaceAll("^\"|\"$", ""); After executing this example, occurrences of double quotes at the beginning or at end of the String will be replaced by empty strings.
How do you remove Triple quotes from a string in Python?In Python triple-quoted strings, you can put a backslash (" \ ") at the end of a line to ignore the line break. In other words, you can use it to put a line break at that spot in your code without making a line break in your string.
|