将单引号字符串转换为双引号字符串
我想检查给定的字符串是单引号还是双引号。如果它是单引号,我想将其转换为双引号,否则它必须是相同的双引号。
I want to check whether the given string is single- or double-quoted. If it is single quote I want to convert it to be double quote, else it has to be same double quote.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
Python 中的“单引号”和“双引号”字符串没有区别:
两者都在内部解析为字符串对象。
我的意思是:
内部是字符串对象。
但是,您可能意味着在字符串对象内添加额外的引号,以便内容本身在打印/导出时显示为引号?
如果您在字符串中混合了引号,例如:
那么您可以使用“替换”方法将所有引号转换为一种类型:
如果您碰巧有嵌套字符串,则首先将现有引号替换为转义引号,然后将其他引用:
There is no difference between "single quoted" and "double quoted" strings in Python:
both are parsed internally to string objects.
I mean:
Are internally string objects.
However you might mean to add an extra quote inside an string object, so that the content itself show up quoted when printed/exported?
If you have a mix of quotes inside a string like:
Then you can use the "replace" method to convert then all into one type:
If you happen to have nested strings, then replace first the existing quotes to escaped quotes, and later the otuer quotes:
听起来您正在使用 JSON。我只是确保它始终是双引号,如下所示:
注意我使用的是
dump
,不是dumps
。Sampledict.json
将如下所示:Sounds like you are working with JSON. I would just make sure it is always a double quoted like this:
Notice I'm using
dump
, notdumps
.Sampledict.json
will look like this:就我而言,我需要以 json 格式打印 list 。
这对我有用:
输出:
之前没有替换:
In my case I needed to print list in json format.
This worked for me:
Output:
Before without replace:
区别仅在于输入。他们是一样的。
您甚至可以这样做:
提供这两种方法很有用,因为您可以让字符串直接包含
'
或"
而无需转义。The difference is only on input. They are the same.
You can even do:
Providing both methods is useful because you can for example have your string contain either
'
or"
directly without escaping.在Python中,单引号或双引号的字符串没有区别,所以我不知道你为什么要这样做。但是,如果您实际上指的是字符串中的单引号字符,那么要将它们替换为双引号,您可以这样做:
mystring.replace('\'', '"')
In Python, there is no difference between strings that are single or double quoted, so I don't know why you would want to do this. However, if you actually mean single quote characters inside a string, then to replace them with double quotes, you would do this:
mystring.replace('\'', '"')
实际上,据我所知,上面的答案都没有回答这个问题,即如何将单引号字符串转换为双引号字符串的问题,无论 python 是否可以互换,都可以使用 Python 自动生成代码,而不能互换。
一个例子是尝试生成一条 SQL 语句,其中使用哪些引号可能非常重要,而且双引号和单引号之间的简单替换可能并不那么简单(即,您可能将双引号括在单引号中)。
返回:
目前我对这个特定问题没有明确的答案,但我认为值得指出,以防有人知道。 (如果我弄清楚的话我会回来的)
Actually, none of the answers above as far as I know answers the question, the question how to convert a single quoted string to a double quoted one, regardless if for python is interchangeable one can be using Python to autogenerate code where is not.
One example can be trying to generate a SQL statement where which quotes are used can be very important, and furthermore a simple replace between double quote and single quote may not be so simple (i.e., you may have double quotes enclosed in single quotes).
Which returns:
At the moment I do not have a clear answer for this particular question but I thought worth pointing out in case someone knows. (Will come back if I figure it out though)
如果您正在讨论在字符串内转换引号,您可以做的一件事就是在结果字符串中将单引号替换为双引号并使用它。像这样的东西:
If you're talking about converting quotes inside a string, One thing you could do is replace single quotes with double quotes in the resulting string and use that. Something like this:
如果您在这里是因为您有一个
dict
对象,您使用str()
将其转换为字符串,并且希望恢复字典,请执行json.loads (str_stuffs.replace("'", '"'))
应该有帮助。例如,希望这有帮助!
If you're here because you have have a
dict
object that you converted to a string usingstr()
and would like the dictionary back, doingjson.loads(str_stuffs.replace("'", '"'))
should help. So for example,Hope this helps!