简体   繁体   English

如何轻松使用包含单引号和双引号的python字符串?

[英]How to easily work with python strings containing both single and double quotes?

How to easily work with python strings containing both single and double quotes? 如何轻松使用包含单引号和双引号的python字符串?

I do have some complex bash lines that do contain: single quotes, double quotes and escapes too and I do want to migrate them to python and I am looking for an easy way to do this, that would minimize the changes needed to these lines, ideally to keep them bash-compatible. 我确实有一些复杂的bash行,其中包含:单引号,双引号和转义符,我也确实想将它们迁移到python,我正在寻找一种简便的方法来做到这一点,以最大程度地减少这些行所需的更改,理想情况下,使其保持与bash兼容。

Example: 例:

  #/bin/bash
  psql -h hostname db -c "\copy (select sr, id, rc AS jql from sr UNION select 'quickfilter', \"ID\", \"QUERY\" from \"AO_60DB71_QUICKFILTER\") TO xxx.csv"

As you can see it contains " , ' , escapes too... making quite ugly to re-escpaes them when moving them to python. 如您所见,它也包含"'转义符...将它们移至python时很难重新隐藏它们。

您可以在r前缀中使用三引号,以防止反斜杠具有特殊含义:

r'''\copy (select sr, id, rc AS jql from sr UNION select 'quickfilter', "ID", "QUERY" from "AO_60DB71_QUICKFILTER") TO xxx.csv'''

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

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