How can I strip first and last double quotes?

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

I want to strip double quotes from:

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

to obtain:

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


        
13条回答
  •  广开言路
    2020-12-02 10:52

    I have some code that needs to strip single or double quotes, and I can't simply ast.literal_eval it.

    if len(arg) > 1 and arg[0] in ('"\'') and arg[-1] == arg[0]:
        arg = arg[1:-1]
    

    This is similar to ToolmakerSteve's answer, but it allows 0 length strings, and doesn't turn the single character " into an empty string.

提交回复
热议问题