How can I strip first and last double quotes?

后端 未结 13 1236
抹茶落季
抹茶落季 2020-12-02 10:14

I want to strip double quotes from:

string = \'\"\" \" \" \"\"\\\\1\" \" \"\" \"\"\'

to obtain:

string = \'\" \" \" \"\"\\\         


        
相关标签:
13条回答
  • 2020-12-02 10:58

    IMPORTANT: I'm extending the question/answer to strip either single or double quotes. And I interpret the question to mean that BOTH quotes must be present, and matching, to perform the strip. Otherwise, the string is returned unchanged.

    To "dequote" a string representation, that might have either single or double quotes around it (this is an extension of @tgray's answer):

    def dequote(s):
        """
        If a string has single or double quotes around it, remove them.
        Make sure the pair of quotes match.
        If a matching pair of quotes is not found, return the string unchanged.
        """
        if (s[0] == s[-1]) and s.startswith(("'", '"')):
            return s[1:-1]
        return s
    

    Explanation:

    startswith can take a tuple, to match any of several alternatives. The reason for the DOUBLED parentheses (( and )) is so that we pass ONE parameter ("'", '"') to startswith(), to specify the permitted prefixes, rather than TWO parameters "'" and '"', which would be interpreted as a prefix and an (invalid) start position.

    s[-1] is the last character in the string.

    Testing:

    print( dequote("\"he\"l'lo\"") )
    print( dequote("'he\"l'lo'") )
    print( dequote("he\"l'lo") )
    print( dequote("'he\"l'lo\"") )
    

    =>

    he"l'lo
    he"l'lo
    he"l'lo
    'he"l'lo"
    

    (For me, regex expressions are non-obvious to read, so I didn't try to extend @Alex's answer.)

    0 讨论(0)
  • 2020-12-02 10:59

    in your example you could use strip but you have to provide the space

    string = '"" " " ""\\1" " "" ""'
    string.strip('" ')  # output '\\1'
    

    note the \' in the output is the standard python quotes for string output

    the value of your variable is '\\1'

    0 讨论(0)
  • 2020-12-02 11:01

    If string is always as you show:

    string[1:-1]
    
    0 讨论(0)
  • 2020-12-02 11:04

    If you can't assume that all the strings you process have double quotes you can use something like this:

    if string.startswith('"') and string.endswith('"'):
        string = string[1:-1]
    

    Edit:

    I'm sure that you just used string as the variable name for exemplification here and in your real code it has a useful name, but I feel obliged to warn you that there is a module named string in the standard libraries. It's not loaded automatically, but if you ever use import string make sure your variable doesn't eclipse it.

    0 讨论(0)
  • 2020-12-02 11:05

    To remove the first and last characters, and in each case do the removal only if the character in question is a double quote:

    import re
    
    s = re.sub(r'^"|"$', '', s)
    

    Note that the RE pattern is different than the one you had given, and the operation is sub ("substitute") with an empty replacement string (strip is a string method but does something pretty different from your requirements, as other answers have indicated).

    0 讨论(0)
  • 2020-12-02 11:08

    find the position of the first and the last " in your string

    >>> s = '"" " " ""\\1" " "" ""'
    >>> l = s.find('"')
    >>> r = s.rfind('"')
    
    >>> s[l+1:r]
    '" " " ""\\1" " "" "'
    
    0 讨论(0)
提交回复
热议问题