Pab*_*her 1160 python string multiline multilinestring
我有一个很长的查询.我想在Python中将它分成几行.在JavaScript中实现它的一种方法是使用几个句子并将它们与+
运算符连接(我知道,也许这不是最有效的方法,但我并不关心这个阶段的性能,只是代码可读性) .例:
var long_string = 'some text not important. just garbage to' +
'illustrate my example';
Run Code Online (Sandbox Code Playgroud)
我尝试在Python中做类似的事情,但它不起作用,所以我习惯\
拆分长字符串.但是,我不确定这是否是唯一/最好/最好的方式.看起来很尴尬.实际代码:
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
Run Code Online (Sandbox Code Playgroud)
Lev*_*von 1994
你在谈论多线字符串吗?轻松,使用三重引号来开始和结束它们.
s = """ this is a very
long string if I had the
energy to type more and more ..."""
Run Code Online (Sandbox Code Playgroud)
您也可以使用单引号(当然在开始和结束时使用其中3个)并将结果字符串视为s
任何其他字符串.
注意:就像任何字符串一样,起始引号和结束引号之间的任何内容都成为字符串的一部分,因此该示例有一个前导空格(由@ root45指出).该字符串还包含空格和换行符.
IE中:
' this is a very\n long string if I had the\n energy to type more and more ...'
Run Code Online (Sandbox Code Playgroud)
最后,还可以在Python中构造长行,如下所示:
s = ("this is a very"
"long string too"
"for sure ..."
)
Run Code Online (Sandbox Code Playgroud)
这将不包括任何额外的空格或换行符(这是一个故意的例子,显示跳过空格会产生什么效果):
'this is a verylong string toofor sure ...'
Run Code Online (Sandbox Code Playgroud)
不需要逗号,只需将要连接在一起的字符串放入一对括号中,并确保考虑任何所需的空格和换行符.
Jes*_*sse 167
如果您不想要多行字符串但只需要一个长单行字符串,则可以使用括号,只要确保字符串段之间不包含逗号,那么它将是一个元组.
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)
Run Code Online (Sandbox Code Playgroud)
在像你正在构建的SQL语句中,多行字符串也可以.但是如果多行字符串包含的额外空格会有问题,那么这将是实现您想要的好方法.
amp*_*ent 130
\
为我打破工作线.这是一个例子:
longStr = "This is a very long string " \
"that I wrote to help somebody " \
"who had a question about " \
"writing long strings in Python"
Run Code Online (Sandbox Code Playgroud)
Eer*_*nen 48
我发现自己对这个感到满意:
string = """This is a
very long string,
containing commas,
that I split up
for readability""".replace('\n',' ')
Run Code Online (Sandbox Code Playgroud)
dar*_*ine 36
我发现在构建长字符串时,通常会执行类似构建SQL查询的操作,在这种情况下,这是最好的:
query = ' '.join(( # note double parens, join() takes an iterable
"SELECT foo",
"FROM bar",
"WHERE baz",
))
Run Code Online (Sandbox Code Playgroud)
Levon建议什么是好的,但可能容易受到错误的影响:
query = (
"SELECT foo"
"FROM bar"
"WHERE baz"
)
query == "SELECT fooFROM barWHERE baz" # probably not what you want
Run Code Online (Sandbox Code Playgroud)
Ste*_*ica 31
PEP 8 风格指南建议使用括号:
换行长行的首选方法是在圆括号、方括号和大括号内使用 Python 的隐式续行。通过将表达式括在括号中,可以将长行分成多行。应优先使用这些内容而不是使用反斜杠来继续行。
例子:
long_string = (
"This is a lengthy string that takes up a lot of space. I am going to "
"keep on typing words to fill up more and more space to show how you can "
"split the string across multiple lines."
)
Run Code Online (Sandbox Code Playgroud)
gjg*_*jgj 28
您还可以在使用""符号时连接变量:
foo = '1234'
long_string = """fosdl a sdlfklaskdf as
as df ajsdfj asdfa sld
a sdf alsdfl alsdfl """ + foo + """ aks
asdkfkasdk fak"""
Run Code Online (Sandbox Code Playgroud)
编辑:找到一个更好的方法,使用命名参数和.format():
body = """
<html>
<head>
</head>
<body>
<p>Lorem ipsum.</p>
<dl>
<dt>Asdf:</dt> <dd><a href="{link}">{name}</a></dd>
</dl>
</body>
</html>
""".format(
link='http://www.asdf.com',
name='Asdf',
)
print(body)
Run Code Online (Sandbox Code Playgroud)
Chr*_*uns 23
这种方法使用:
inspect
模块剥离局部缩进account_id
和def_id
变量.这种方式看起来对我来说是最诡异的.
# import textwrap # See update to answer below
import inspect
# query = textwrap.dedent(f'''\
query = inspect.cleandoc(f'''
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}'''
)
Run Code Online (Sandbox Code Playgroud)
更新:1/29/2019合并@ ShadowRanger的建议使用inspect.cleandoc
而不是textwrap.dedent
Vla*_*den 22
在Python> = 3.6中,您可以使用格式化字符串文字(f string)
query= f'''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}'''
Run Code Online (Sandbox Code Playgroud)
pan*_*ang 16
例如:
sql = ("select field1, field2, field3, field4 "
"from table "
"where condition1={} "
"and condition2={}").format(1, 2)
Output: 'select field1, field2, field3, field4 from table
where condition1=1 and condition2=2'
Run Code Online (Sandbox Code Playgroud)
如果condition的值应该是一个字符串,你可以这样做:
sql = ("select field1, field2, field3, field4 "
"from table "
"where condition1='{0}' "
"and condition2='{1}'").format('2016-10-12', '2017-10-12')
Output: "select field1, field2, field3, field4 from table where
condition1='2016-10-12' and condition2='2017-10-12'"
Run Code Online (Sandbox Code Playgroud)
use*_*855 16
添加到@Levon的答案......
1. 创建一个多行字符串,如下所示:
paragraph = """this is a very
long string if I had the
energy to type more and more ..."""
print(paragraph)
Run Code Online (Sandbox Code Playgroud)
输出:
'this is a very\n long string if I had the\n energy to type more and more ...'
Run Code Online (Sandbox Code Playgroud)
该字符串将包含换行符和空格。所以删除它们。
2.使用正则表达式删除多余的空格
paragraph = re.sub('\s+', ' ', paragraph)
print(paragraph)
Run Code Online (Sandbox Code Playgroud)
输出:
'this is a very long string if I had the energy to type more and more ...'
Run Code Online (Sandbox Code Playgroud)
Fah*_*mad 10
我个人发现以下是在Python中编写原始SQL查询的最佳(简单,安全和Pythonic)方法,尤其是在使用Python的sqlite3模块时:
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 = ?
AND record_def.account_id = ?
AND def_id = ?
'''
vars = (account_id, account_id, def_id) # a tuple of query variables
cursor.execute(query, vars) # using Python's sqlite3 module
Run Code Online (Sandbox Code Playgroud)
?
占位符替换,因此?
当查询中存在大量Python变量时,跟踪哪个Python变量可能会变得有点困难.小智 10
作为在 Python 中处理长字符串的一般方法,您可以使用三重引号,split
并且join
:
_str = ' '.join('''Lorem ipsum dolor sit amet, consectetur adipiscing
elit, sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo.'''.split())
Run Code Online (Sandbox Code Playgroud)
输出:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo.'
Run Code Online (Sandbox Code Playgroud)
关于 OP 与 SQL 查询相关的问题,下面的答案忽略了这种构建 SQL 查询的方法的正确性,只关注以可读和美观的方式构建长字符串,而无需额外导入。它还忽略了这带来的计算负载。
使用三重引号,我们构建了一个长且可读的字符串,然后我们将其分解为一个列表,split()
从而去除空白,然后用' '.join()
. 最后,我们使用以下format()
命令插入变量:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo.'
Run Code Online (Sandbox Code Playgroud)
产生:
account_id = 123
def_id = 321
_str = '''
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 = {}
AND record_def.account_id = {}
AND def_id = {}
'''
query = ' '.join(_str.split()).format(account_id, account_id, def_id)
Run Code Online (Sandbox Code Playgroud)
这种方法不符合PEP 8,但我发现它有时很有用。
请注意,原始字符串中的大括号由 format() 函数使用。
我觉得textwrap.dedent
最好的长字符串描述在这里:
def create_snippet():
code_snippet = textwrap.dedent("""\
int main(int argc, char* argv[]) {
return 0;
}
""")
do_something(code_snippet)
Run Code Online (Sandbox Code Playgroud)
小智 7
其他人已经提到了括号方法,但是我想在括号中添加,允许内联注释。
nursery_rhyme = (
'Mary had a little lamb,' # Comments are great!
'its fleece was white as snow.'
'And everywhere that Mary went,'
'her sheep would surely go.' # What a pesky sheep.
)
Run Code Online (Sandbox Code Playgroud)
当使用反斜杠连续行(\
)时,不允许注释。您会收到一个SyntaxError: unexpected character after line continuation character
错误消息。
nursery_rhyme = 'Mary had a little lamb,' \ # These comments
'its fleece was white as snow.' \ # are invalid!
'And everywhere that Mary went,' \
'her sheep would surely go.'
# => SyntaxError: unexpected character after line continuation character
Run Code Online (Sandbox Code Playgroud)
根据https://docs.python.org/3/library/re.html#re.VERBOSE的示例,
a = re.compile(
r'\d+' # the integral part
r'\.' # the decimal point
r'\d*' # some fractional digits
)
Run Code Online (Sandbox Code Playgroud)
# Using VERBOSE flag, IDE usually can't syntax highight the string comment.
a = re.compile(r"""\d + # the integral part
\. # the decimal point
\d * # some fractional digits""", re.X)
Run Code Online (Sandbox Code Playgroud)
小智 6
我通常使用这样的东西:
text = '''
This string was typed to be a demo
on how could we write a multi-line
text in Python.
'''
Run Code Online (Sandbox Code Playgroud)
如果你想删除每行中烦人的空格,你可以这样做:
text = '\n'.join(line.lstrip() for line in text.splitlines())
Run Code Online (Sandbox Code Playgroud)
嗯。
我知道这个问题发布已经很长时间了。但我刚刚找到了我想要用来将长多行字符串分配给项目中的变量的样式。这需要一些额外的运行时间,但仍然保留了代码的美观,即使我分配字符串的变量是大量缩进的。
# Suppose the following code is heavily indented
line3header = "Third"
variable = fr"""
First line.
Second line.
{line3header} line.
{{}} line.
...
The last line.
""".strip()
"""A variable whose name is Variable.
You can even add a docstring here.
"""
variable = variable.format("Fourth")
print(variable)
variable += "\n"
print(variable, end="")
Run Code Online (Sandbox Code Playgroud)
就这样。
你的实际代码不应该工作;您在“行”的末尾缺少空格(例如,role.descr as roleFROM...
)。
多行字符串有三重引号:
string = """line
line2
line3"""
Run Code Online (Sandbox Code Playgroud)
它将包含换行符和额外的空格,但对于 SQL 这不是问题。
小智 5
尝试这样的事情。就像这种格式一样,它将返回一条连续的行,就像您已成功查询此属性一样:
"message": f'You have successfully inquired about '
f'{enquiring_property.title} Property owned by '
f'{enquiring_property.client}'
Run Code Online (Sandbox Code Playgroud)
tl;dr:使用"""\
和"""
包裹字符串,如
string = """\
This is a long string
spanning multiple lines.
"""
Run Code Online (Sandbox Code Playgroud)
来自官方 Python 文档:
字符串文字可以跨越多行。一种方法是使用三引号:"""...""" 或 '''...'''。行尾会自动包含在字符串中,但可以通过在行尾添加 \ 来防止出现这种情况。下面的例子:
print("""\
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
""")
Run Code Online (Sandbox Code Playgroud)
产生以下输出(注意不包括初始换行符):
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
Run Code Online (Sandbox Code Playgroud)
结合以下想法:
根据我的格式建议,您可以将查询编写为:
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
)
vars = (account_id, account_id, def_id) # A tuple of the query variables
cursor.execute(query, vars) # Using Python's sqlite3 module
Run Code Online (Sandbox Code Playgroud)
或者像:
vars = []
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 = '
vars.append(account_id) or '?'
' AND'
' record_def.account_id = '
vars.append(account_id) or '?'
' AND'
' def_id = '
vars.append(def_id) or '?'
)
cursor.execute(query, tuple(vars)) # Using Python's sqlite3 module
Run Code Online (Sandbox Code Playgroud)
与 'IN' 和 'vars.extend(options) 或 n_options(len(options))' 一起使用可能会很有趣,其中:
def n_options(count):
return '(' + ','.join(count*'?') + ')'
Run Code Online (Sandbox Code Playgroud)
或者根据darkfeline的提示,您可能仍然会在使用那些前导空格和分隔符以及命名占位符时犯错误:
SPACE_SEP = ' '
COMMA_SEP = ', '
AND_SEP = ' AND '
query = SPACE_SEP.join((
'SELECT',
COMMA_SEP.join((
'action.descr as "action"',
'role.id as role_id',
'role.descr as role',
)),
'FROM',
COMMA_SEP.join((
'public.role_action_def',
'public.role',
'public.record_def',
'public.action',
)),
'WHERE',
AND_SEP.join((
'role.id = role_action_def.role_id',
'record_def.id = role_action_def.def_id',
'action.id = role_action_def.action_id',
'role_action_def.account_id = :account_id',
'record_def.account_id = :account_id',
'def_id = :def_id',
)),
))
vars = {'account_id':account_id,'def_id':def_id} # A dictionary of the query variables
cursor.execute(query, vars) # Using Python's sqlite3 module
Run Code Online (Sandbox Code Playgroud)
请参阅Cursor.execute-function 的文档。
“这是[最Pythonic]的方式!” - ...
归档时间: |
|
查看次数: |
1022032 次 |
最近记录: |