Hướng dẫn how do you span a string with multiple lines in python? - làm thế nào để bạn kéo dài một chuỗi có nhiều dòng trong python?

kết hợp ý tưởng từ:

Levon hoặc Jesse, Fahel và Ddrscott

Với đề xuất định dạng của tôi, bạn không thể viết truy vấn của mình như:

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

hoặc thích:

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

Điều này có thể thú vị cùng với 'in' và 'vars.extend (tùy chọn) hoặc n_options (Len (Len (Len (Len))', trong đó:

def n_options(count):
    return '(' + ','.join(count*'?') + ')'

Hoặc với gợi ý từ Darkfeline, rằng bạn vẫn có thể phạm sai lầm với những không gian và phân tách của các nhà lãnh đạo và cả với các địa điểm ghi nhớ:

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

Xem tài liệu của chương trình giảng dạy.

"Đây là cách [Pythonic] nhất!" -


Chuỗi đa dòng

Bạn có thể gán một chuỗi đa dòng cho một biến bằng cách sử dụng ba trích dẫn:

Thí dụ

Bạn có thể sử dụng ba trích dẫn kép:

A = "" "Lorem ipsum dor SIT AMET, lãnh đạo adipiscing elit, sed do eiusmod tempor inciduntut labore et dolore magna aliqua." "" Print (a)
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."""
print(a)

Hãy tự mình thử »

Hoặc ba trích dẫn đơn:

Thí dụ

A = '' 'Lorem ipsum dor SIT AMET, lãnh sự quảng cáo elit, sed do eiusmod tempor inciduntut labore et dolore magna aliqua.' '' in (a)
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.'''
print(a)

Hãy tự mình thử »

LƯU Ý: Trong kết quả, các lần phá vỡ dòng được chèn ở cùng một vị trí như trong mã. in the result, the line breaks are inserted at the same position as in the code.




Khi sử dụng các trình kiểm tra mã PEP8 như Flake8 trong Python, một lỗi,

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
2, được nêu ra khi một dòng vượt quá 80 ký tự.

Bài viết này mô tả cách viết một chuỗi dài không chứa một dòng mới trên nhiều dòng.

  • Sử dụng dấu gạch chéo ngược (
    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
    
    3) làm ký tự tiếp tục dòng
  • Sử dụng dấu ngoặc đơn

Xem bài viết sau đây cho các hoạt động khác nhau liên quan đến các chuỗi với các lần ngắt dòng.

  • Xử lý các lần phá vỡ dòng (Newlines) trong Python

Nếu bạn muốn bọc hoặc cắt các chuỗi dài, mô -đun TextWrap rất hữu ích. Xem bài viết sau đây.

  • Bọc và cắt ngắn một chuỗi với textwrap trong python

Nếu số lượng ký tự trong một dòng trở nên quá dài do chuỗi phương thức, bạn có thể phá vỡ dòng theo cùng một cách.

  • Chuỗi phương pháp với sự phá vỡ dòng trong Python

Sử dụng dấu gạch chéo ngược (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 3) làm ký tự tiếp tục dòng

Sử dụng dấu ngoặc đơn

n = 1 + 2 \
    + 3

print(n)
# 6

Xem bài viết sau đây cho các hoạt động khác nhau liên quan đến các chuỗi với các lần ngắt dòng.

s = 'aaa' 'bbb'

print(s)
# aaabbb

Xử lý các lần phá vỡ dòng (Newlines) trong Python

s = 'https://ja.wikipedia.org/wiki/'\
    '%E3%83%97%E3%83%AD%E3%82%B0%E3%83'\
    '%A9%E3%83%9F%E3%83%B3%E3%82%B0%E8%A8%80%E8%AA%9E'

print(s)
# https://ja.wikipedia.org/wiki/%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0%E8%A8%80%E8%AA%9E

Nếu bạn muốn bọc hoặc cắt các chuỗi dài, mô -đun TextWrap rất hữu ích. Xem bài viết sau đây.

s_var = 'xxx'

# s = 'aaa' s_var 'bbb'
# SyntaxError: invalid syntax

Bọc và cắt ngắn một chuỗi với textwrap trong python

s = 'aaa' + s_var + 'bbb'

print(s)
# aaaxxxbbb

Nếu số lượng ký tự trong một dòng trở nên quá dài do chuỗi phương thức, bạn có thể phá vỡ dòng theo cùng một cách.

s = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\
    + s_var\
    + 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'

print(s)
# aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaxxxbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

Chuỗi phương pháp với sự phá vỡ dòng trong Python

  • Trong Python, một dấu gạch chéo ngược (
    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
    
    3) là một nhân vật tiếp tục dòng. Nếu một dấu gạch chéo ngược được đặt ở cuối một dòng, người ta coi là dòng được tiếp tục trên dòng tiếp theo.

Sử dụng dấu ngoặc đơn

Xem bài viết sau đây cho các hoạt động khác nhau liên quan đến các chuỗi với các lần ngắt dòng.

Xử lý các lần phá vỡ dòng (Newlines) trong Python

  • Một tuple với một yếu tố yêu cầu một dấu phẩy trong Python

Bạn có thể viết như sau.

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
0

Nếu các biến được bao gồm, bạn cần toán tử

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
8.

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
1