本文翻译自:Pythonic way to create a long multi-line string
I have a very long query. 我有一个很长的查询。 I would like to split it in several lines in Python. 我想在Python中将其分成几行。 A way to do it in JavaScript would be using several sentences and joining them with a +
operator (I know, maybe it's not the most efficient way to do it, but I'm not really concerned about performance in this stage, just code readability). 用JavaScript做到这一点的一种方法是使用几个句子,然后用+
运算符将它们连接起来(我知道,这可能不是最有效的方法,但是我并不真正关心此阶段的性能,只是代码可读性)。 Example: 例:
var long_string = 'some text not important. just garbage to' +
'illustrate my example';
I tried doing something similar in Python, but it didn't work, so I used \\
to split the long string. 我尝试在Python中执行类似的操作,但没有成功,因此我使用\\
拆分了长字符串。 However, I'm not sure if this is the only/best/pythonicest way of doing it. 但是,我不确定这是否是唯一/最佳/最佳的方法。 It looks awkward. 看起来很尴尬。 Actual code: 实际代码:
query = 'SELECT action.descr as "action", '\
'role.id as role_id,'\
'role.descr as role'\
'FROM '\
'public.role_action_def,'\
'public.role,'\
'public.record_def, '\
'public.action'\
'WHERE role.id = role_action_def.role_id AND'\
'record_def.id = role_action_def.def_id AND'\
'action.id = role_action_def.action_id AND'\
'role_action_def.account_id = ' + account_id + ' AND'\
'record_def.account_id=' + account_id + ' AND'\
'def_id=' + def_id
#1楼
参考:https://stackoom.com/question/ijGV/创建长的多行字符串的Pythonic方法
#2楼
Are you talking about multi-line strings? 您在谈论多行字符串吗? Easy, use triple quotes to start and end them. 容易,使用三引号将它们开始和结束。
s = """ this is a very
long string if I had the
energy to type more and more ..."""
You can use single quotes too (3 of them of course at start and end) and treat the resulting string s
just like any other string. 您也可以使用单引号(当然在开始和结束时使用3个单引号),并将生成的字符串s
与其他任何字符串一样对待。
NOTE : Just as with any string, anything between the starting and ending quotes becomes part of the string, so this example has a leading blank (as pointed out by @root45). 注意 :与任何字符串一样,引号和结尾引号之间的任何内容都将成为字符串的一部分,因此本示例中有一个前导空格(如@ root45所指出)。 This string will also contain both blanks and newlines. 该字符串还将包含空格和换行符。
Ie,: 即:
' this is a very\n long string if I had the\n energy to type more and more ...'
Finally, one can also construct long lines in Python like this: 最后,还可以像这样在Python中构造长行:
s = ("this is a very"
"long string too"
"for sure ..."
)
which will not include any extra blanks or newlines (this is a deliberate example showing what the effect of skipping blanks will result in): 其中将不包含任何额外的空格或换行符(这是一个有意的示例,显示了跳过空格会导致什么结果):
'this is a verylong string toofor sure ...'
No commas required, simply place the strings to be joined together into a pair of parenthesis and be sure to account for any needed blanks and newlines. 不需要逗号,只需将要连接的字符串放在一对括号中,并确保考虑到任何需要的空格和换行符。
#3楼
Your actual code shouldn't work, you are missing whitespaces at the end of "lines" (eg: role.descr as roleFROM...
) 您的实际代码不起作用,在“行”末尾缺少空格(例如: role.descr as roleFROM...
)
There is triplequotes for multiline string: 多行字符串有三引号:
string = """line
line2
line3"""
It will contain the line breaks and extra spaces, but for SQL that's not a problem. 它将包含换行符和多余的空格,但是对于SQL来说这不是问题。
#4楼
If you don't want a multiline string but just have a long single line string, you can use parentheses, just make sure you don't include commas between the string segments, then it will be a tuple. 如果您不希望使用多行字符串,而只需要一个长的单行字符串,则可以使用括号,只需确保在字符串段之间不包含逗号,那么它将是一个元组。
query = ('SELECT action.descr as "action", '
'role.id as role_id,'
'role.descr as role'
' FROM '
'public.role_action_def,'
'public.role,'
'public.record_def, '
'public.action'
' WHERE role.id = role_action_def.role_id AND'
' record_def.id = role_action_def.def_id AND'
' action.id = role_action_def.action_id AND'
' role_action_def.account_id = '+account_id+' AND'
' record_def.account_id='+account_id+' AND'
' def_id='+def_id)
In a SQL statement like what you're constructing, multiline strings would also be fine. 在您正在构造的SQL语句中,多行字符串也可以。 But if the extra whitespace a multiline string would contain would be a problem, then this would be a good way to achieve what you want. 但是,如果多行字符串将包含额外的空格将是一个问题,那么这将是实现所需功能的好方法。
#5楼
You can also place the sql-statement in a seperate file action.sql
and load it in the py file with 您还可以将sql语句放置在单独的文件action.sql
并使用以下命令将其加载到py文件中:
with open('action.sql') as f:
query = f.read()
So the sql-statements will be separated from the python code. 因此,sql语句将与python代码分开。 If there are parameters in the sql statement which needs to be filled from python, you can use string formating (like %s or {field}) 如果sql语句中有需要从python填充的参数,则可以使用字符串格式(例如%s或{field})
#6楼
I found myself happy with this one: 我发现自己对此很满意:
string = """This is a
very long string,
containing commas,
that I split up
for readability""".replace('\n',' ')