From 63f02713e0a3cbd92ebbd660d684783f3f787116 Mon Sep 17 00:00:00 2001 From: "gowthaman.b" Date: Tue, 13 Jun 2023 09:33:48 +0530 Subject: [PATCH] add readme and add csv creation --- .gitignore | 3 ++- README.md | 3 +-- main.py | 21 ++++++++++++++++++++- requirements.txt | 3 ++- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index ee62f67..e019059 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.db *.json -.idea/ \ No newline at end of file +.idea/ +*.csv \ No newline at end of file diff --git a/README.md b/README.md index 16d25ce..241f508 100644 --- a/README.md +++ b/README.md @@ -10,5 +10,4 @@ Steps * install required packages `pip install -r requirements.txt` * run `python3 main.py` * if successful will create a transactions.db (sqlite database) -* can be used with anyother tool to convert to csv -* one way to export to csv is is `https://www.sqlitetutorial.net/sqlite-export-csv/` \ No newline at end of file +* also create csv diff --git a/main.py b/main.py index 55e409a..b39f796 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,9 @@ # This is a sample Python script. import ijson import sqlite3 +import pandas as pd +from glob import glob +from os.path import expanduser # Press ⌃R to execute it or replace it with your code. # Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings. @@ -97,6 +100,20 @@ def write_to_database(tbl, cols): cur.execute(sql, values) +def make_csv_from_tables(): + cur.execute("SELECT name FROM sqlite_master WHERE type='table'") + tbls = [] + + for (tbl,) in cur.fetchall(): + print("tbl = ", tbl) + if tbl.find("transactions") == 0: + tbls.append(tbl) + + for tbl in tbls: + clients = pd.read_sql(f"SELECT * FROM {tbl}", con) + clients.to_csv(f"{tbl}.csv", index=False) + + def parse_json(): extract_keys_names = extract_keys.keys() headers = [] @@ -141,7 +158,9 @@ def parse_json(): flat_json[k] = item[k] write_to_database(parent, flat_json) - con.commit() + con.commit() + + make_csv_from_tables() # Press the green button in the gutter to run the script. diff --git a/requirements.txt b/requirements.txt index 9981eae..635843b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -ijson \ No newline at end of file +ijson +pandas \ No newline at end of file