繁体   English   中英

Python中的两个单双引号

[英]two single double quotes in Python

我有一个字符串列表,但我想在每个字符串前后加上单双引号。 如果我有名单

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

我想把它改成:

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

我怎样才能用列表理解或其他方法来做到这一点? 我想要两组单引号。 我希望将此传递给 redshift 以将查询结果卸载到 s3。https://docs.aws.amazon.com/redshift/latest/dg/r_UNLOAD.html

像这样尝试:

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

好吧,python 语法不允许

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

我们必须使用转义字符\\"\\' ;在您的情况下,因为您需要两个: \\"\\"\\'\\'

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

现在以下内容存储在words

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

你也可以使用用双引号包裹的 f 字符串并通常添加单引号(我不是说这是正确的,但它确实有效)

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

也许我误解了您,但您不能只使用双引号来表示字符串表示法并使用单引号表示字符串内容吗?,然后只需与+连接:

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