string - python如何删除第一个和最后一个双引号

我想从中删除双引号

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

成为

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

我试图使用rstrip,lstrip和strip('[^\"]|[\"$]'),但它没有用。

我怎样才能做到这一点?感谢你们对我的帮助。

Walapa asked 2019-08-26T02:37:14Z

12个解决方案

166 votes

如果你要剥离的报价总是"第一个也是最后一个" 如你所说,那么你可以简单地使用:

string = string[1:-1]
houbysoft answered 2019-08-26T02:37:33Z
84 votes

如果您不能假设您处理的所有字符串都有双引号,您可以使用以下内容:

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

编辑:

我确定您刚刚使用string作为示例的变量名称,在您的实际代码中它有一个有用的名称,但我不得不警告您标准库中有一个名为string的模块。 它没有自动加载,但是如果您使用过import string,请确保您的变量不会被删除。

tgray answered 2019-08-26T02:38:07Z
41 votes

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

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

请注意,RE模式与您给出的模式不同,操作是sub("替换"),其中包含空替换字符串(strip是一种字符串方法,但执行的操作与您的要求完全不同,如 其他答案已表明)。

Alex Martelli answered 2019-08-26T02:38:41Z
37 votes

重要提示:我将问题/答案扩展为删除单引号或双引号。 我解释这个问题意味着BOTH引号必须存在并匹配才能执行条带。 否则,字符串将保持不变。

去除#qu;" dequote" 一个字符串表示,可能有单引号或双引号(这是@tgray答案的扩展):

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可以使用元组,以匹配任何一种替代方案。 DOUBLED括号s[-1]和))的原因是我们将一个参数("'", '"')传递给startswith(),以指定允许的前缀,而不是TWO参数"'"和'"',它们将被解释为前缀和(无效)起始位置。

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的回答。)

ToolmakerSteve answered 2019-08-26T02:39:44Z
10 votes

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

string[1:-1]
Larry answered 2019-08-26T02:40:11Z
8 votes

几乎完成了。 引自[http://docs.python.org/library/stdtypes.html?highlight=strip#str.strip]

chars参数是一个字符串   指定要设置的字符集  除去。

[...]

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

所以论证不是正则表达式。

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

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

pihentagy answered 2019-08-26T02:41:09Z
4 votes

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

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

要么

string = string[1:-1]
TooAngel answered 2019-08-26T02:41:38Z
2 votes

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

s = '/Hello World/'
s.strip('/')
> 'Hello World'
nsantana answered 2019-08-26T02:42:05Z
1 votes

我有一些代码需要删除单引号或双引号,我不能只是ast.literal_eval它。

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

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

dbn answered 2019-08-26T02:42:41Z
0 votes

找到第一个和最后一个的位置。 在你的字符串中

>>> s = '"" " " ""\\1" " "" ""'
>>> l = s.find('"')
>>> r = s.rfind('"')
>>> s[l+1:r]
'" " " ""\\1" " "" "'
remosu answered 2019-08-26T02:43:08Z
0 votes

在你的例子中你可以使用strip但你必须提供空间

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

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

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

RomainL. answered 2019-08-26T02:43:51Z
-1 votes

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

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
Sumer answered 2019-08-26T02:44:18Z