Hướng dẫn python split multiline string - chuỗi nhiều dòng phân chia python

Kết hợp các ý tưởng từ:

Levon hoặc Jesse, Faheel và Ddrscott

Với đề xuất định dạng của tôi, bạn có 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 (tùy chọn))', trong đó: 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 hàng đầu đó và cả với các khoản giữ chỗ được đặt tên:

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 con trỏ.Execute-function.

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

Kết hợp các ý tưởng từ:

Nội phân chính

  • Sử dụng dấu gạch chéo ngược (\) làm ký tự tiếp tục dòng
  • Sử dụng dấu ngoặc đơn
  • Làm thế nào để bạn phá vỡ một chuỗi với nhiều dòng?
  • Làm thế nào để bạn chia một chuỗi trong Python?
  • Làm thế nào để bạn thực hiện nhiều dòng trong Python?

Làm thế nào để đưa ra ý kiến ​​đa dòng trong Python. Không giống như các ngôn ngữ lập trình khác như JavaScript, Java và C ++ sử dụng / *... * / cho các nhận xét đa dòng, không có cơ chế tích hợp nào cho các nhận xét đa dòng trong Python. Để nhận xét nhiều dòng trong Python, bạn có thể dành cho mỗi dòng bằng một hàm băm ( #).

Kết hợp các ý tưởng từ:

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

Nội phân chính

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

Levon hoặc Jesse, Faheel và Ddrscott

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

Với đề xuất định dạng của tôi, bạn có thể viết truy vấn của mình 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

Hoặc thích:

Đ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 (tùy chọn))', trong đó: trong đó:

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 hàng đầu đó và cả với các khoản giữ chỗ được đặt tên:

Xem tài liệu của con trỏ.Execute-function.

  • "Đây là cách [Pythonic] nhất!" - ...
  • Sử dụng dấu ngoặc đơn

Trong Python, bạn có thể tự do phá vỡ dòng trong ngoặc đơn (

def n_options(count):
    return '(' + ','.join(count*'?') + ')'
4,
def n_options(count):
    return '(' + ','.join(count*'?') + ')'
5,
def n_options(count):
    return '(' + ','.join(count*'?') + ')'
6). Sử dụng quy tắc này, bạn có thể viết một chuỗi dài trên nhiều dòng với dấu ngoặc đơn thay vì dấu gạch chéo ngược.

  • def n_options(count):
        return '(' + ','.join(count*'?') + ')'
    
    5 được sử dụng cho
    def n_options(count):
        return '(' + ','.join(count*'?') + ')'
    
    8 và
    def n_options(count):
        return '(' + ','.join(count*'?') + ')'
    
    6 được sử dụng cho
    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
    
    0, nên sử dụng
    def n_options(count):
        return '(' + ','.join(count*'?') + ')'
    
    4 cho mục đích đó. Lưu ý rằng
    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
    
    2 được tạo bởi dấu phẩy, không phải
    def n_options(count):
        return '(' + ','.join(count*'?') + ')'
    
    4.

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

  • 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 (\) làm ký tự tiếp tục dòng

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

n = 1 + 2 \
    + 3

print(n)
# 6

Ngoài ra, nếu nhiều chuỗi chữ được viết tuần tự, chúng được nối thành một chuỗi như sau:

s = 'aaa' 'bbb'

print(s)
# aaabbb

Do đó, bạn có thể viết một chuỗi dài thành nhiều dòng 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

Chỉ có chuỗi chữ (chuỗi được bao quanh bở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
9 hoặc
def n_options(count):
    return '(' + ','.join(count*'?') + ')'
0) được nối nếu được viết liên tiếp. Lưu ý rằng trong trường hợp biến, một lỗi được nêu ra.

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

Sử dụng toán tử

def n_options(count):
    return '(' + ','.join(count*'?') + ')'
1 để kết hợp các biến hoặc biến và chuỗi 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
2

Bạn cần toán tử

def n_options(count):
    return '(' + ','.join(count*'?') + ')'
1 để kết nối các biến, ngay cả khi chúng được phân tách bằ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
7).

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

Xem bài viết sau đây để biết chi tiết về nối chuỗi.

  • Chuỗi Concatenate trong Python (+ toán tử, tham gia, v.v.)

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

Trong Python, bạn có thể tự do phá vỡ dòng trong ngoặc đơn (

def n_options(count):
    return '(' + ','.join(count*'?') + ')'
4,
def n_options(count):
    return '(' + ','.join(count*'?') + ')'
5,
def n_options(count):
    return '(' + ','.join(count*'?') + ')'
6). Sử dụng quy tắc này, bạn có thể viết một chuỗi dài trên nhiều dòng với dấu ngoặc đơn thay vì dấu gạch chéo ngược.

def n_options(count):
    return '(' + ','.join(count*'?') + ')'
5 được sử dụng cho
def n_options(count):
    return '(' + ','.join(count*'?') + ')'
8 và
def n_options(count):
    return '(' + ','.join(count*'?') + ')'
6 được sử dụng cho
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
0, nên sử dụng
def n_options(count):
    return '(' + ','.join(count*'?') + ')'
4 cho mục đích đó. Lưu ý rằng
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
2 được tạo bởi dấu phẩy, không phải
def n_options(count):
    return '(' + ','.join(count*'?') + ')'
4.

  • 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
4

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

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

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
5

Làm thế nào để bạn phá vỡ một chuỗi với nhiều dòng?

Bạn có thể sử dụng phương thức .Split () và phương thức .join () với nhau để chia một chuỗi thành nhiều dòng.use the . split() method and the . join() method together to split a string into multiple lines.

Làm thế nào để bạn chia một chuỗi trong Python?

Phương thức chia () chia một chuỗi vào một danh sách. Bạn có thể chỉ định phân tách, dấu phân cách mặc định là bất kỳ khoảng trắng nào. Lưu ý: Khi MaxSplit được chỉ định, danh sách sẽ chứa số lượng phần tử được chỉ định cộng với một.. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

Làm thế nào để bạn thực hiện nhiều dòng trong Python?

Làm thế nào để đưa ra ý kiến ​​đa dòng trong Python. Không giống như các ngôn ngữ lập trình khác như JavaScript, Java và C ++ sử dụng / *... * / cho các nhận xét đa dòng, không có cơ chế tích hợp nào cho các nhận xét đa dòng trong Python. Để nhận xét nhiều dòng trong Python, bạn có thể dành cho mỗi dòng bằng một hàm băm ( #).prepend each line with a hash ( # ).