如何去掉第一个和最后一个双引号?
我想从: 中删除双引号
string = '"" " " ""\\1" " "" ""'
以获得:
string = '" " " ""\\1" " "" "'
我尝试使用 rstrip、lstrip 和 strip('[^\"]|[\"$] ') 但它不起作用。
我该怎么做?
I want to strip double quotes from:
string = '"" " " ""\\1" " "" ""'
to obtain:
string = '" " " ""\\1" " "" "'
I tried to use rstrip
, lstrip
and strip('[^\"]|[\"$]')
but it did not work.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
如果您要删除的引号始终如您所说的“第一个和最后一个”,那么您可以简单地使用:
string = string[1:-1]
If the quotes you want to strip are always going to be "first and last" as you said, then you could simply use:
string = string[1:-1]
如果您不能假设您处理的所有字符串都有双引号,您可以使用如下内容:
编辑:
我确定您刚刚使用了
string
作为变量此处举例说明,在您的真实代码中它有一个有用的名称,但我觉得有必要警告您有一个名为字符串
。它不会自动加载,但如果您曾经使用导入字符串
,请确保您的变量不会掩盖它。If you can't assume that all the strings you process have double quotes you can use something like this:
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 namedstring
in the standard libraries. It's not loaded automatically, but if you ever useimport string
make sure your variable doesn't eclipse it.重要提示:我正在扩展问题/答案以去掉单引号或双引号。我将这个问题解释为两个引号都必须存在并且匹配才能执行剥离。否则,字符串原样返回。
要“取消引用”字符串表示形式,该字符串表示形式可能带有单引号或双引号(这是 @tgray 答案的扩展):
说明:
startswith
可以采用一个元组,以匹配多个替代方案中的任何一个。使用双括号((
和))
的原因是我们将一个参数("'", '"')
传递给 < code>startswith(),指定允许的前缀,而不是两个参数"'"
和'"'
,这两个参数将被解释为前缀(无效的)起始位置。s[-1]
是字符串中的最后一个字符。测试:
=>
(对我来说,正则表达式读起来并不明显,所以我没有尝试扩展@Alex的答案。)
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):
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("'", '"')
tostartswith()
, 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:
=>
(For me, regex expressions are non-obvious to read, so I didn't try to extend @Alex's answer.)
要删除第一个和最后一个字符,并且在每种情况下仅当相关字符是双引号时才删除:
请注意,RE 模式与您给出的模式不同,并且操作是
sub (“替换”)带有空替换字符串(
strip
是一种字符串方法,但其功能与您的要求完全不同,正如其他答案所示)。To remove the first and last characters, and in each case do the removal only if the character in question is a double quote:
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).如果字符串始终如您所示:
If string is always as you show:
快完成了。引用自 http://docs.python.org/library/stdtypes .html?highlight=strip#str.strip
[...]
所以参数不是正则表达式。
请注意,这并不完全是您所要求的,因为它会占用字符串两端的多个引号!
Almost done. Quoting from http://docs.python.org/library/stdtypes.html?highlight=strip#str.strip
[...]
So the argument is not a regexp.
Note, that this is not exactly what you requested, because it eats multiple quotes from both end of the string!
从字符串的开头和结尾删除确定的字符串。
Remove a determinated string from start and end from a string.
从
Python 3.9
开始,您可以使用removeprefix
和删除后缀
:Starting in
Python 3.9
, you can useremoveprefix
andremovesuffix
:如果您确定要删除开头和结尾处有一个“,只需执行以下操作:
或
If you are sure there is a " at the beginning and at the end, which you want to remove, just do:
or
我有一些代码需要去掉单引号或双引号,并且我不能简单地对其进行 ast.literal_eval 。
这与 ToolmakerSteve 的答案类似,但它允许 0 长度的字符串,并且不会将单个字符
"
转换为空字符串。I have some code that needs to strip single or double quotes, and I can't simply ast.literal_eval it.
This is similar to ToolmakerSteve's answer, but it allows 0 length strings, and doesn't turn the single character
"
into an empty string.在你的例子中你可以使用 strip 但你必须提供空格
注意输出中的 \' 是字符串输出的标准 python 引号
变量的值是 '\\1'
in your example you could use strip but you have to provide the space
note the \' in the output is the standard python quotes for string output
the value of your variable is '\\1'
下面的函数将去除空空格并返回不带引号的字符串。如果没有引号,那么它将返回相同的字符串(删除)
Below function will strip the empty spces and return the strings without quotes. If there are no quotes then it will return same string(stripped)
找到字符串中第一个和最后一个 " 的位置
find the position of the first and the last " in your string