Python定义大型查询字符串列表与从文件读取

时间:2019-06-10 19:33:24

标签: python python-3.x string list file-io

我对使用python中的大字符串列表时的最佳实践以及性能会感到好奇。具体来说,我有一个包含以字符串形式包含不同postgreSQL查询的列表,我想知道有关如何初始化列表的最佳实践。请考虑以下两种方法:

方法1-用代码创建列表:

query_load = [("SELECT val_1, COUNT(*) as frequency "
     "FROM table "
     "GROUP BY val_1 "
     "ORDER BY val_1 ASC"),

     ("SELECT val_2, COUNT(*) as frequency "
     "FROM table "
     "GROUP BY val_2 "
     "ORDER BY val_2 ASC"),

     ("SELECT val_3, COUNT(*) as frequency "
     "FROM table "
     "GROUP BY val_3 "
     "ORDER BY val_3 ASC"),

     ("SELECT val_4, COUNT(*) as frequency "
     "FROM table "
     "GROUP BY val_4 "
     "ORDER BY val_4 ASC"), 
     ... 
      ]

方法2-将查询从文件读取到列表中

my_list = [line.rstrip('\n') for line in open("..Desktop/my_queries.txt")]

就可读性和更少的代码行而言,方法2似乎是更好的选择,但我希望与最佳实践保持一致。另外,将文件逐行读入列表是否会导致性能下降(数量不菲)?

1 个答案:

答案 0 :(得分:1)

只需使用三引号引起来的字符串即可; SQL不会在意用于使Python源代码可读的空白。

query_load = [
    """
    SELECT val_1, COUNT(*) as frequency
    FROM table
    GROUP BY val_1
    ORDER BY val_1 ASC
    """,

    """
    SELECT val_2, COUNT(*) as frequency
    FROM table
    GROUP BY val_2
    ORDER BY val_2 ASC
    """,

    # etc
]

您是对查询进行硬编码还是从外部文件中读取它们,实际上是一个单独的问题,仅与源代码的外观紧密相关。