简体   繁体   English

三引号内的 Python 参数

[英]Python arguments inside triple quotes

I have a python script that contains a sql query.我有一个包含 sql 查询的 python 脚本。 I use triple quotes around the sql query for formatting purposes.出于格式化目的,我在 sql 查询周围使用三重引号。 I'd like to inject variables that I populate from the command line into the query.我想将从命令行填充的变量注入到查询中。 How can I do this while preserving the triple quotes.如何在保留三重引号的同时做到这一点。 Are there better ways to get around the triple quotes?有没有更好的方法来绕过三重引号?

Ex:前任:

AGE = raw_input("Enter your age: ")

vdf = vertica.select_dataframe("""
    Select
        col1
        col2
        coln
    FROM
        TableX
    WHERE
        col2 IN (21, 22, AGE)
    Group BY 1
""")

You can use format like this: 您可以使用如下格式

AGE = raw_input("Enter your age: ")
query_1 = """
    Select
        col1
        col2
        coln
    FROM
        TableX
    WHERE
        col2 IN (21, 22, {})
    Group BY 1
"""
vdf = vertica.select_dataframe(query_1.format(AGE))

A simple example with triple quotes and multiple assignments is: 一个带有三重引号和多个赋值的简单示例是:

>>> age = 100
>>> name = "koukouviou"
>>> """I am {} and I am {} years old""".format(name, age)
'I am koukouviou and I am 100 years old'

You could make the query string separate and use format to put correct age, eg: 您可以将查询字符串分开,并使用格式设置正确的年龄,例如:

a_query = """
    Select
        col1
        col2
        coln
    FROM
        TableX
    WHERE
        col2 IN (21, 22, {})
    Group BY 1
"""

vdf = vertica.select_dataframe(a_query.format(AGE))

I am surprised, that the fabulous % operator is not mentioned, pythons build in string formatting would make your original lines work with a tiny modification: 我很惊讶,没有提到神话般的%运算符,以字符串格式构建的python将使您的原始行进行微小的修改即可工作:

AGE = raw_input("Enter your age: ")

vdf = vertica.select_dataframe("""
    Select
        col1,
        col2,
        coln
    FROM
        TableX
    WHERE
        col2 IN (21, 22, %s)
    Group BY 1
""" % AGE)

This would also work for queries with multiple arguments: 这也适用于具有多个参数的查询:

AGE = raw_input("Enter your age: ")
GENDER = raw_input("Enter your gender (m/f): ")
HEIGHT = raw_input("Enter your height in cm: ")

vdf = vertica.select_dataframe("""
    INSERT INTO stats (
        age,
        gender,
        height
    )
    VALUES
    (
        '%s',
        '%s',
        '%s'
    )
""" % ( AGE, GENDER, HEIGHT ))

I will add one more answer with a dict unpacking as it made my life much simpler. 我将再加上一个dict再加上一个答案,因为它使我的生活变得更加简单。 Using @hexerei-software's INSERT example: 使用@ hexerei-software的INSERT示例:

AGE = raw_input("Enter your age: ")
GENDER = raw_input("Enter your gender (m/f): ")
HEIGHT = raw_input("Enter your height in cm: ")

settings = {'AGE': AGE, 'GENDER': GENDER, 'HEIGHT': HEIGHT}

vdf = vertica.select_dataframe("""
    INSERT INTO stats (
        age,
        gender,
        height
    )
    VALUES
    (
        '{AGE}',
        '{GENDER}',
        '{HEIGHT}'
    )
""".format(**settings))

I prefer using f-Strings !我更喜欢使用f-Strings

vdf = vertica.select_dataframe(f"""
    Select
        col1
        col2
        coln
    FROM
        TableX
    WHERE
        col2 IN (21, 22, {AGE})
    Group BY 1
""")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM