简体   繁体   中英

Is there a quick way to return a string without its escape sequences in Python?

I want to be able to tell if a string foo == 'some string'. This works most of the time. I realize, however, that there may be times when foo contains escape sequences such as '\\n' or '\\t', and I want to account for this. Is there anything quick, or built-in to Python 2.7 that will help me with this? Or will I have to essentially go through all escape sequences and make sure none of them are infesting my string foo?

Here's an example if you're still unsure:

foo = '\tZebra'

So when I print foo it appears as

    Zebra

and I can't easily be sure that there are no escape sequences such as '\\t' when testing foo against a string literal:

foo == 'Zebra'

returns False.

I thought of is using these lines:

if 'Zebra' in foo:
    bar()

but this accounts for MORE than just escape sequences of Python. For example:

foo = 'ttZebra'
if 'Zebra' in foo:
    print 'bar'

this will indeed print 'bar'.

So, how can I quickly remove all escape sequences from a string before I use it? Also, if this helps, I know that none of my strings will contain spaces because they all come from a .split() list.


Answer:

I tried using .strip(), and that helped, but my program still wasn't working. It turns out all of my files have UTF-8 BOM s. However, the BOM sequence is always the same so it is very easy to deal with. I still use .strip() to account for all escape sequences.

If you're asking about stripping any sequences from the beginning and end of the string, then use strip()

>>> foo = '\tZebra'
>>> foo.strip()
>>> 'Zebra'

If you want it to strip in the middle of the string as well, you can do the following

>>> import re
>>> re.sub('[\x00-\x1F\x7F]', '', '\tZebra\tZebra')
'ZebraZebra'

The above regular expression strips out all control characters .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM