创建33个变量而无需在python中手动编写它们

时间:2016-02-04 11:24:51

标签: variables python-3.x flask sqlite

我正在创建33个执行db查询的变量,我知道必须有一种方法可以让它更简单,然后手动编写33行代码。我现在正在做的是:

    allrtd = c.execute("SELECT Amsterdam from rtdtimes")
    allrtd1 = c.execute("SELECT Bucharest from rtdtimes")
    allrtd2 = c.execute("SELECT Barcelona from rtdtimes")
    allrtd3 = c.execute("SELECT Berlin from rtdtimes")
    allrtd4 = c.execute("SELECT Bratislava from rtdtimes")
    allrtd5 = c.execute("SELECT Budapest from rtdtimes")
    allrtd6 = c.execute("SELECT Copenhagen from rtdtimes")
    allrtd7 = c.execute("SELECT Dublin from rtdtimes")
    allrtd8 = c.execute("SELECT Dusseldorf from rtdtimes")
    allrtd9 = c.execute("SELECT Dubai from rtdtimes")
    allrtd10 = c.execute("SELECT Florence from rtdtimes")
    allrtd11 = c.execute("SELECT Frankfurt from rtdtimes")
    allrtd12 = c.execute("SELECT Geneva from rtdtimes")
    allrtd13 = c.execute("SELECT Hamburg from rtdtimes")
    allrtd14 = c.execute("SELECT HongKong from rtdtimes")
    allrtd15 = c.execute("SELECT Istanbul from rtdtimes")
    allrtd16 = c.execute("SELECT LosAngeles from rtdtimes")
    allrtd17 = c.execute("SELECT London from rtdtimes")
    allrtd18 = c.execute("SELECT Madrid from rtdtimes")
    allrtd19 = c.execute("SELECT Milan from rtdtimes")
    allrtd20 = c.execute("SELECT Marseille from rtdtimes")
    allrtd21 = c.execute("SELECT Moscow from rtdtimes")
    allrtd22 = c.execute("SELECT Munich from rtdtimes")
    allrtd23 = c.execute("SELECT NewYork from rtdtimes")
    allrtd24 = c.execute("SELECT Paris from rtdtimes")
    allrtd25 = c.execute("SELECT Prague from rtdtimes")
    allrtd26 = c.execute("SELECT Rotterdam from rtdtimes")
    allrtd27 = c.execute("SELECT Sofia from rtdtimes")
    allrtd28 = c.execute("SELECT Stockholm from rtdtimes")
    allrtd29 = c.execute("SELECT Venice from rtdtimes")
    allrtd30 = c.execute("SELECT Vienna from rtdtimes")
    allrtd31 = c.execute("SELECT Warsaw from rtdtimes")
    allrtd32 = c.execute("SELECT Zurich from rtdtimes")

我有一个包含所有城市名称的列表,如下所示:

city1 = ['Amsterdam', 'Bucharest', 'Barcelona', 'Berlin', 'Bratislava', 'Budapest',
            'Copenhagen', 'Dublin', 'Dusseldorf', 'Dubai', 'Florence', 'Frankfurt',
            'Geneva', 'Hamburg', 'HongKong', 'Istanbul', 'LosAngeles', 'London',
            'Madrid', 'Milan', 'Marseille', 'Moscow', 'Munich', 'NewYork',
            'Paris', 'Prague', 'Rotterdam', 'Sofia', 'Stockholm', 'Venice', 'Vienna',
            'Warsaw', 'Zurich']

我也在使用Flask,所以我需要在模板中渲染这个变量,以便我可以在html文件中使用这些变量。

现在我正在渲染它:

return render_template("Allresults.html", city1=city1, allrtd=allrtd,
                                       allrtd1=allrtd1, allrtd2=allrtd2, allrtd3=allrtd3, allrtd4=allrtd4,
                                       allrtd5=allrtd5, allrtd6=allrtd6, allrtd7=allrtd7, allrtd8=allrtd8,
                                       allrtd9=allrtd9, allrtd10=allrtd10, allrtd11=allrtd11, allrtd12=allrtd12,
                                       allrtd13=allrtd13, allrtd14=allrtd14, allrtd15=allrtd15, allrtd16=allrtd16,
                                       allrtd17=allrtd17, allrtd18=allrtd18, allrtd19=allrtd19, allrtd20=allrtd20,
                                       allrtd21=allrtd21, allrtd22=allrtd22, allrtd23=allrtd23, allrtd24=allrtd24,
                                       allrtd25=allrtd25, allrtd26=allrtd26, allrtd27=allrtd27, allrtd28=allrtd28,
                                       allrtd29=allrtd29, allrtd30=allrtd30, allrtd31=allrtd31, allrtd32=allrtd32)

我不知道如何使用字典来完成此操作,因为重复的问题与我正在做的事情非常不同。

1 个答案:

答案 0 :(得分:0)

您可以从这里获得自定义jinja2过滤器:

@app.template_filter('city_data')
def city_data(city_name):
    return c.execute("SELECT {city} from rtdtimes".format(city=city_name))

@app.route('/')
def index():

    return render_template("Allresults.html", cities=city1)

然后,在您的Allresults.html模板中使用:

{% for city in cities %}

    {{ city|city_data }}

{% endfor %} 

但是,要非常小心,因为如果您允许用户指定城市,您将自己向SQL injection attacks开放。