Hướng dẫn python multi-line variable assignment - chuyển nhượng biến nhiều dòng python

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

Nội phân chính

  • Một biến chuỗi đa dòng
  • Làm thế nào để bạn mã hóa nhiều dòng trong Python?
  • Làm thế nào để bạn gán nhiều giá trị cho một biến trong Python?
  • Làm thế nào để bạn thêm các dòng vào một biến trong Python?
  • Làm thế nào để bạn thêm nhiều dòng vào một tệp trong Python?

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!" - ...

Onyejiaku Theophilus Chidalu

Tổng quan

Một biến trong Python là một vị trí bộ nhớ dành riêng được sử dụng để lưu trữ các giá trị. Ví dụ:variable in Python is a reserved memory location used to store values. For example:

name = "Theo"

name là một biến trong khi "Theo" là giá trị biến đổi.

Từ mã trên, biến name cũng có thể được gọi rõ ràng là biến chuỗi vì giá trị được gán cho nó,

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, là loại dữ liệu hoặc giá trị
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.

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 trong mã là một chuỗi dòng duy nhất. Điều gì xảy ra khi chúng ta muốn viết hai hoặc ba câu?

Thí dụ

# trying to create the multiline string variable

composition = "Theo is a good boy.

His favorite food is fried rice and chicken.

He likes to plat basket ball as well as football"

# to return the multiline string variable

print[composition]

Cố gắng tạo một chuỗi đa dòng

Đầu ra

SyntaxError: EOL while scanning string literal

Thật không may, mã của chúng tôi đã trả lại mộ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
4 và đọc thêm
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.

Điều này có nghĩa là trình thông dịch Python mong đợi một ký tự hoặc tập hợp chuỗi các ký tự sẽ tiếp tục trong dòng mã cụ thể đó. Tuy nhiên, các ký tự hoặc chuỗi các ký tự đó không thể được tìm thấy tại phần cuối của dòng [

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
6 là viết tắt của kết thúc dòng]. Vì lý do đó, Python trả về một thông báo lỗi.

Một biến chuỗi đa dòng

Để tạo một biến chuỗi đa dòng trong mã Python, để tránh thông báo lỗi, chúng tôi sẽ gửi chuỗi đa dòng trong ba trích dẫn đơn [

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] hoặc ba trích dẫn kép [
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]

Thí dụ

# creating a correct multiline string

name = """Theo is a good boy.

His favorite food is fried rice with chicken.

He likes to play basketball as well as football."""

# to return the multiline string variable

print[name]

Cố gắng tạo một chuỗi đa dòng

Đầu ra

Thật không may, mã của chúng tôi đã trả lại mộ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
4 và đọc thêm
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.

Điều này có nghĩa là trình thông dịch Python mong đợi một ký tự hoặc tập hợp chuỗi các ký tự sẽ tiếp tục trong dòng mã cụ thể đó. Tuy nhiên, các ký tự hoặc chuỗi các ký tự đó không thể được tìm thấy tại phần cuối của dòng [

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
6 là viết tắt của kết thúc dòng]. Vì lý do đó, Python trả về một thông báo lỗi.

Onyejiaku Theophilus Chidalu

Làm thế nào để bạn mã hóa nhiều dòng trong Python?

Tổng quanuse the backslash [ \ ] to indicate that a statement is continued on the next line. In the revised version of the script, a blank space and an underscore indicate that the statement that was started on line 1 is continued on line 2.

Làm thế nào để bạn gán nhiều giá trị cho một biến trong Python?

Làm thế nào để bạn thêm các dòng vào một biến trong Python?by using = consecutively. This is useful, for example, when initializing multiple variables to the same value. It is also possible to assign another value into one after assigning the same value.

Làm thế nào để bạn thêm các dòng vào một biến trong Python?

Làm thế nào để bạn thêm nhiều dòng vào một tệp trong Python?specify the newline character by "n". The "" is called the escape character used for mentioning whitespace characters such as t, n and r. Mentioning the newline character using n will bring the cursor to the consecutive line.

Làm thế nào để bạn thêm nhiều dòng vào một tệp trong Python?

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ư:

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!" - ...

print[content].

Bài Viết Liên Quan

Chủ Đề