Skip to content
Open
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions access_parser/access_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,15 @@ def parse(self):
if record:
self._parse_row(record)
return self.parsed_table

def dump(self):
"""
Convert table structure to list of dictionaries.
:return defaultdict(list) from parsed table
"""
parsed_table = self.parse()
rows = list(zip(*parsed_table.values(), strict=False))
return [dict(zip(parsed_table.keys(), row)) for row in rows]

def _parse_row(self, record):
"""
Expand Down
21 changes: 20 additions & 1 deletion examples/parse_db.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from access_parser import AccessParser
from tabulate import tabulate
import json
import argparse


Expand All @@ -16,12 +17,30 @@ def print_tables(db_path, only_catalog=False, specific_table=None):
else:
db.print_database()

def dump_tables(db_path, specific_table=None):
db = AccessParser(db_path)
if specific_table:
print(f'TABLE NAME: {specific_table}\r\n')
table = db.get_table(specific_table)
print(table.dump())
else:
for table_name in db.catalog.keys():
print(f'TABLE NAME: {table_name}\r\n')
table = db.get_table(table_name)
dumped_table = table.dump()
for row_number, row in enumerate(dumped_table):
print(f'{row_number}:\t {row}')
print("\n\n\n\n")

if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--dump", action="store_true", required=False, help="Dumps to a dictionary object", default=None)
parser.add_argument("-c", "--catalog", required=False, help="Print DB table names", action="store_true")
parser.add_argument("-f", "--file", required=True, help="*.mdb / *.accdb File")
parser.add_argument("-t", "--table", required=False, help="Table to print", default=None)

args = parser.parse_args()
print_tables(args.file, args.catalog, args.table)
if args.dump:
dump_tables(args.file)
else:
print_tables(args.file, args.catalog, args.table)