简体   繁体   English

Python中的两个单双引号

[英]two single double quotes in Python

I have a list of strings but I want to make single double quotes before and after each string.我有一个字符串列表,但我想在每个字符串前后加上单双引号。 If I have the list如果我有名单

words = ['abc', 'acd', 'edf']

I want to change it to:我想把它改成:

words = [''abc'', ''cdf'', ''edf'']

How can I do it with list comprehension or other method?我怎样才能用列表理解或其他方法来做到这一点? I want two sets of single quotation marks.我想要两组单引号。 I want this to pass to redshift to unload the query result to s3.我希望将此传递给 redshift 以将查询结果卸载到 s3。https://docs.aws.amazon.com/redshift/latest/dg/r_UNLOAD.htmlhttps://docs.aws.amazon.com/redshift/latest/dg/r_UNLOAD.html

Try like this:像这样尝试:

words = ['abc', 'acd', 'edf']
words = ['\'\'{}\'\''.format(x) for x in words]
print(words)

well python syntax won't allow好吧,python 语法不允许

words = [''abc'', ''cdf'', ''edf'']

we have to use escape characters \\" or \\' ; in your case since you want two: \\"\\" or \\'\\'我们必须使用转义字符\\"\\' ;在您的情况下,因为您需要两个: \\"\\"\\'\\'

words = ['abc', 'cdf', 'edf']
words = [f"\'\'{word}\'\'" for word in words]

now the following is stored in words :现在以下内容存储在words

["''abc''", "''cdf''", "''edf''"]

you can also use f strings wrapped with double quotations and normally add single quotations (im not saying this is proper but it does work)你也可以使用用双引号包裹的 f 字符串并通常添加单引号(我不是说这是正确的,但它确实有效)

words = [f"''{word}''" for word in words]
["''abc''", "''cdf''", "''edf''"]

Maybe I am misunderstanding you but cant you just use double quotes to signify the string notation and use the single quotes for the string content?, then just concatenate with + :也许我误解了您,但您不能只使用双引号来表示字符串表示法并使用单引号表示字符串内容吗?,然后只需与+连接:

words = ['abc', 'acd', 'edf']
new_list = ["''"+item+"''" for item in words]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM