简体   繁体   English

通过传递脚本名称在 Python 中执行多个脚本

[英]Execute multiple scripts in Python by passing the scripts names

I have a total of 25-30 scripts that updates data on the server.我总共有 25-30 个脚本来更新服务器上的数据。 Since every day new data gets generated, so every day I have to run these scripts manually in a sequence and update the database with new data.由于每天都会生成新数据,因此每天我都必须按顺序手动运行这些脚本并使用新数据更新数据库。 All these scripts contain 300+ lines of SQL queries.所有这些脚本都包含 300 多行 SQL 查询。

Now I want to automate this task with Python, but not really sure how to do that.现在我想用 Python 自动化这个任务,但不确定如何做到这一点。 I have used some libraries in past to connect to SQL server and then define a cursor to execute certain queries - cur.execute(select * from abc)我过去使用过一些库连接到 SQL 服务器,然后定义一个游标来执行某些查询 - cur.execute(select * from abc)

But now I want to automate this task and run all scripts by passing just the script names.但现在我想自动化这个任务并通过只传递脚本名称来运行所有脚本。 Like喜欢

cur.execute(sql_script_1)
cur.execute(sql_script_2)
cur.execute(sql_script_3)
.
.
.
cur.execute(sql_script_25)

In this way, in the end, I'll just have to run this .py file and it will automatically run all scripts in the given order.这样,最后,我只需要运行这个 .py 文件,它就会按照给定的顺序自动运行所有脚本。 Can this be done somehow?这可以以某种方式完成吗? Either in this way or some other way.以这种方式或其他方式。

The main motive is to automate the task of running all scripts by just passing the names.主要动机是通过传递名称来自动化运行所有脚本的任务。

Your question is probably a bad one, kinda vague and no effort shown in terms of research or implementing it yourself.您的问题可能是一个糟糕的问题,有点模糊,并且在研究或自己实施方面没有表现出任何努力。 But it's possible I had a similar use case, so I will share.但我可能有类似的用例,所以我会分享。

In my case I needed to pull down data as a pandas df.在我的情况下,我需要将数据作为熊猫 df 提取。 Your case might vary but I imagine basic structure will remain same.您的情况可能会有所不同,但我想基本结构将保持不变。

In any case here is what I did:无论如何,这就是我所做的:

  1. you store each of the sql scripts as a string variable in a python file (or files) somewhere in your project directory.您将每个 sql 脚本作为字符串变量存储在项目目录中某处的 python 文件(或多个文件)中。

  2. you define a function that manages the connection and passes a sql script as an argument.您定义了一个管理连接的函数,并将一个 sql 脚本作为参数传递。

  3. you call that function as needed for each script.您可以根据需要为每个脚本调用该函数。

So your python file in one looks something like:所以你的python文件合二为一:

sql_script_1 = 'select....from...where...'
sql_script_2 = 'select....from...where...'
sql_script_3 = 'select....from...where...'

Then you define the function in two to manage the connection.然后你定义两个函数来管理连接。

Something like就像是

import pandas as pd
import pyodbc

def query_passer(query:str, conn_db):
    conn = pyodbc.connect(conn_db)
    df = pd.read_sql(query, conn)
    conn.close()
    return df

Then in three you call the function, do whatever you were gonna do with the data, and then repeat for each query.然后在三个中调用该函数,对数据执行您要执行的任何操作,然后对每个查询重复此操作。

The code below assume the query_passer function was saved in a python file named "functions.py" in the subfolder "resources" and that the queries are stored in a file named "query1.py" in the subfolder "resources.queries".下面的代码假设 query_passer 函数保存在子文件夹“resources”中名为“functions.py”的 python 文件中,并且查询存储在子文件夹“resources.queries”中名为“query1.py”的文件中。 Organize your own files as you wish, or just keep them all in one big file.根据需要整理您自己的文件,或者将它们全部保存在一个大文件中。

from resources.functions import query_passer
from resources.queries.query1 import sql_script_1, sql_script_2, sql_script_3
import pandas as pd

# define the connection 
conn_db = (
                "Driver={stuff};"
                "Server=stuff;"
                "Database=stuff;"
                ".....;"
)
# run queries
df = query_passer(sql_script_1, conn_db)
df.to_csv('sql_script_1.csv')

df = query_passer(sql_script_2, conn_db)
df.to_csv('sql_script_2.csv')

df = query_passer(sql_script_3, conn_db)
df.to_csv('sql_script_3.csv')

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

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