112

我想从以下内容中去除双引号:

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

获得:

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

我尝试使用rstriplstripstrip('[^\"]|[\"$]')它不起作用。

我怎样才能做到这一点?

4

13 回答 13

204

如果您要删除的引号总是如您所说的“第一个和最后一个”,那么您可以简单地使用:

string = string[1:-1]

于 2010-06-21T14:15:56.940 回答
104

如果你不能假设你处理的所有字符串都有双引号,你可以使用这样的东西:

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

编辑:

我确定您只是string在这里用作示例的变量名,并且在您的实际代码中它有一个有用的名称,但我不得不警告您string在标准库中命名了一个模块。它不会自动加载,但如果您曾经使用过,请import string确保您的变量不会使其黯然失色。

于 2010-06-21T15:20:58.940 回答
53

重要提示:我正在扩展问题/答案以去除单引号或双引号。我将这个问题解释为意味着两个引号都必须存在并且匹配,才能执行剥离。否则,字符串原封不动地返回。

要“取消引用”一个字符串表示,它可能有单引号或双引号(这是@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

解释:

startswith可以采用一个元组来匹配多个备选方案中的任何一个。使用双括号((and的原因))是我们将一个参数传递("'", '"')startswith(), 以指定允许的前缀,而不是两个参数"'"and '"',这将被解释为前缀和(无效的)起始位置。

s[-1]是字符串中的最后一个字符。

测试:

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"

(对我来说,正则表达式的阅读并不明显,所以我没有尝试扩展@Alex 的答案。)

于 2013-12-13T23:12:46.307 回答
49

要删除第一个和最后一个字符,并且在每种情况下,仅当相关字符是双引号时才进行删除:

import re

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

请注意,RE 模式与您给出的模式不同,并且操作是sub("substitute") 与一个空的替换字符串(strip是一种字符串方法,但与您的要求完全不同,正如其他答案所表明的那样)。

于 2010-06-21T15:31:49.180 回答
17

如果字符串始终如您所见:

string[1:-1]
于 2010-06-21T14:16:26.750 回答
8

快完成了。引用自http://docs.python.org/library/stdtypes.html?highlight=strip#str.strip

chars 参数是一个字符串,指定要删除的字符集。

[...]

chars 参数不是前缀或后缀;相反,它的值的所有组合都被剥离:

所以这个论点不是一个正则表达式。

>>> string = '"" " " ""\\1" " "" ""'
>>> string.strip('"')
' " " ""\\1" " "" '
>>> 

请注意,这不完全符合您的要求,因为它会从字符串的两端吃掉多个引号!

于 2010-06-21T15:52:12.360 回答
5

从字符串的开头和结尾删除确定的字符串。

s = '""Hello World""'
s.strip('""')

> 'Hello World'
于 2018-05-17T18:51:20.370 回答
4

如果您确定要删除的开头和结尾有一个 ",请执行以下操作:

string = string[1:len(string)-1]

或者

string = string[1:-1]
于 2010-06-21T14:17:19.517 回答
4

从 开始Python 3.9,您可以使用removeprefixand removesuffix

'"" " " ""\\1" " "" ""'.removeprefix('"').removesuffix('"')
# '" " " ""\\1" " "" "'
于 2020-04-25T21:13:36.190 回答
1

我有一些代码需要去掉单引号或双引号,我不能简单地使用 ast.literal_eval 它。

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

这类似于 ToolmakerSteve 的答案,但它允许长度为 0 的字符串,并且不会将单个字符"变成空字符串。

于 2016-08-26T00:43:16.357 回答
0

下面的函数将去除空的 spces 并返回不带引号的字符串。如果没有引号,那么它将返回相同的字符串(剥离)

def removeQuote(str):
str = str.strip()
if re.search("^[\'\"].*[\'\"]$",str):
    str = str[1:-1]
    print("Removed Quotes",str)
else:
    print("Same String",str)
return str
于 2018-03-03T11:21:59.243 回答
0

在您的示例中,您可以使用条带,但您必须提供空间

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

注意输出中的\'是字符串输出的标准python引号

你的变量的值是 '\\1'

于 2016-11-29T11:40:15.850 回答
-1

找到字符串中第一个和最后一个 " 的位置

>>> s = '"" " " ""\\1" " "" ""'
>>> l = s.find('"')
>>> r = s.rfind('"')

>>> s[l+1:r]
'" " " ""\\1" " "" "'
于 2010-06-21T15:46:24.647 回答