Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(create) file 5011_2t #2

Merged
merged 1 commit into from
Nov 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions add_q5011_2t.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import json
import pandas as pd
import psycopg2
import zipfile

from glob import glob


class Q5011_2TUpdater:
def __init__(self):
self.config = self.get_config()

def get_config(self):
with open("config.json", "r", encoding="utf-8") as input_file:
return json.load(input_file)

def get_dataframe(self, file_name):
return pd.read_excel(file_name)

def update_table(self, dataframe):
with psycopg2.connect(
host=self.config["db_host"],
dbname=self.config["db_name"],
user=self.config["db_user"],
password=self.config["db_password"],
) as conn:
with conn.cursor() as cur:
for _, row in dataframe.iterrows():
query_parameters = {
"id": row["ID"],
"q5011_2t": row["Q5011_2T"],
}
cur.execute(
"""
UPDATE recruits_log
SET q5011_2t = %(q5011_2t)s
WHERE id = %(id)s;
""",
query_parameters,
)
conn.commit()

def run(self):
for file_name in glob("./xlsx/*.zip"):
print(file_name[7:])
with zipfile.ZipFile(file_name, 'r') as zip:
with zip.open(zip.namelist()[0]) as excel_file:
dataframe = self.get_dataframe(excel_file)
self.update_table(dataframe)


if __name__ == "__main__":
u = Q5011_2TUpdater()
u.run()