简体   繁体   中英

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?

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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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