Hướng dẫn how do you split a long string in python? - làm thế nào để bạn tách một chuỗi dài trong 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!" - ...

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.

def n_options(count):
    return '(' + ','.join(count*'?') + ')'
2 được sử dụng cho
def n_options(count):
    return '(' + ','.join(count*'?') + ')'
5 và
def n_options(count):
    return '(' + ','.join(count*'?') + ')'
3 được sử dụng cho
def n_options(count):
    return '(' + ','.join(count*'?') + ')'
7, sử dụng
def n_options(count):
    return '(' + ','.join(count*'?') + ')'
1 cho mục đích đó. Lưu ý rằng
def n_options(count):
    return '(' + ','.join(count*'?') + ')'
9 được tạo bởi dấu phẩy, không phải
def n_options(count):
    return '(' + ','.join(count*'?') + ')'
1.

  • 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