将单引号字符串转换为双引号字符串

发布于 2025-01-11 08:25:45 字数 59 浏览 0 评论 0原文

我想检查给定的字符串是单引号还是双引号。如果它是单引号,我想将其转换为双引号,否则它必须是相同的双引号。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

有木有妳兜一样 2025-01-18 08:25:45

Python 中的“单引号”和“双引号”字符串没有区别:
两者都在内部解析为字符串对象。

我的意思是:

a = "European Swallow"
b = 'African Swallow'

内部是字符串对象。

但是,您可能意味着在字符串对象内添加额外的引号,以便内容本身在打印/导出时显示为引号?

c = "'Unladen Swallow'"

如果您在字符串中混合了引号,例如:

a = """ Merry "Christmas"! Happy 'new year'! """

那么您可以使用“替换”方法将所有引号转换为一种类型:

a = a.replace('"', "'") 

如果您碰巧有嵌套字符串,则首先将现有引号替换为转义引号,然后将其他引用:

a = """This is an example: "containing 'nested' strings" """
a = a.replace("'", "\\\'")
a = a.replace('"', "'")

There is no difference between "single quoted" and "double quoted" strings in Python:
both are parsed internally to string objects.

I mean:

a = "European Swallow"
b = 'African Swallow'

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?

c = "'Unladen Swallow'"

If you have a mix of quotes inside a string like:

a = """ Merry "Christmas"! Happy 'new year'! """

Then you can use the "replace" method to convert then all into one type:

a = a.replace('"', "'") 

If you happen to have nested strings, then replace first the existing quotes to escaped quotes, and later the otuer quotes:

a = """This is an example: "containing 'nested' strings" """
a = a.replace("'", "\\\'")
a = a.replace('"', "'")
谈下烟灰 2025-01-18 08:25:45

听起来您正在使用 JSON。我只是确保它始终是双引号,如下所示:

doubleQString = "{0}".format('my normal string')
with open('sampledict.json','w') as f:
    json.dump(doubleQString ,f)

注意我使用的是 dump不是 dumps

Sampledict.json 将如下所示:

"my normal string"

Sounds like you are working with JSON. I would just make sure it is always a double quoted like this:

doubleQString = "{0}".format('my normal string')
with open('sampledict.json','w') as f:
    json.dump(doubleQString ,f)

Notice I'm using dump, not dumps.

Sampledict.json will look like this:

"my normal string"
北音执念 2025-01-18 08:25:45

就我而言,我需要以 json 格式打印 list
这对我有用:

f'''"inputs" : {str(vec).replace("'", '"')},\n'''

输出:

"inputs" : ["Input_Vector0_0_0", "Input_Vector0_0_1"],

之前没有替换:

f'"inputs" : {vec},\n'

"inputs" : ['Input_Vector0_0_0', 'Input_Vector0_0_1'],

In my case I needed to print list in json format.
This worked for me:

f'''"inputs" : {str(vec).replace("'", '"')},\n'''

Output:

"inputs" : ["Input_Vector0_0_0", "Input_Vector0_0_1"],

Before without replace:

f'"inputs" : {vec},\n'

"inputs" : ['Input_Vector0_0_0', 'Input_Vector0_0_1'],
挖个坑埋了你 2025-01-18 08:25:45

区别仅在于输入。他们是一样的。

s = "hi"
t = 'hi'
s == t

正确

您甚至可以这样做:

"hi" == 'hi'

正确

提供这两种方法很有用,因为您可以让字符串直接包含 '" 而无需转义。

The difference is only on input. They are the same.

s = "hi"
t = 'hi'
s == t

True

You can even do:

"hi" == 'hi'

True

Providing both methods is useful because you can for example have your string contain either ' or " directly without escaping.

你げ笑在眉眼 2025-01-18 08:25:45

在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('\'', '"')

莳間冲淡了誓言ζ 2025-01-18 08:25:45

实际上,据我所知,上面的答案都没有回答这个问题,即如何将单引号字符串转换为双引号字符串的问题,无论 python 是否可以互换,都可以使用 Python 自动生成代码,而不能互换。

一个例子是尝试生成一条 SQL 语句,其中使用哪些引号可能非常重要,而且双引号和单引号之间的简单替换可能并不那么简单(即,您可能将双引号括在单引号中)。

print('INSERT INTO xx.xx VALUES' + str(tuple(['a',"b'c",'dfg'])) +';')

返回:

INSERT INTO xx.xx VALUES('a', "b'c", 'dfg');

目前我对这个特定问题没有明确的答案,但我认为值得指出,以防有人知道。 (如果我弄清楚的话我会回来的)

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).

print('INSERT INTO xx.xx VALUES' + str(tuple(['a',"b'c",'dfg'])) +';')

Which returns:

INSERT INTO xx.xx VALUES('a', "b'c", 'dfg');

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)

无所的.畏惧 2025-01-18 08:25:45

如果您正在讨论在字符串内转换引号,您可以做的一件事就是在结果字符串中将单引号替换为双引号并使用它。像这样的东西:

def toDouble(stmt):
    return stmt.replace("'",'"')

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:

def toDouble(stmt):
    return stmt.replace("'",'"')
枕梦 2025-01-18 08:25:45

如果您在这里是因为您有一个 dict 对象,您使用 str() 将其转换为字符串,并且希望恢复字典,请执行 json.loads (str_stuffs.replace("'", '"')) 应该有帮助。例如,

import json

stuffs = {"stuff1": "value", "stuff2": "value", "stuff3": "value"}
str_stuffs = str(stuffs)

original_stuffs = json.loads(str_stuffs.replace("'", '"'))

希望这有帮助!

If you're here because you have have a dict object that you converted to a string using str() and would like the dictionary back, doing json.loads(str_stuffs.replace("'", '"')) should help. So for example,

import json

stuffs = {"stuff1": "value", "stuff2": "value", "stuff3": "value"}
str_stuffs = str(stuffs)

original_stuffs = json.loads(str_stuffs.replace("'", '"'))

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文