Created
September 12, 2024 23:13
-
-
Save doshiraki/0157805ed0a26447fa43981ddbb8074f to your computer and use it in GitHub Desktop.
add cloumns
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from enum import Enum, auto | |
| import os | |
| def list_directories_recursively(path): | |
| for root2, dirs, files in os.walk(path): | |
| root = root2 + "/" | |
| root = root2[len(path) + 1 :] | |
| # 現在のディレクトリ内のサブディレクトリをリストアップ | |
| for file in files: | |
| yield (root, file) | |
| # Enumの定義 | |
| class COLUMN(Enum): | |
| SCHEMA = 0 | |
| TABLE = auto() | |
| COLUMN_NAME = auto() | |
| class Parser: | |
| def __init__(self, file) -> None: | |
| self.file = file | |
| with open(file, "br") as fp: | |
| pretbl = None | |
| tables = dict() | |
| while True: | |
| pos = fp.tell() | |
| line = fp.readline() | |
| if not line: | |
| break | |
| line = line.decode() | |
| columns = line[:-1].split(",") | |
| curtbl = (columns[COLUMN.SCHEMA.value], columns[COLUMN.TABLE.value]) | |
| if curtbl != pretbl: | |
| tables[curtbl] = pos | |
| pretbl = curtbl | |
| self.tablepos = tables | |
| def getColumns(self, schema, table): | |
| targettbl = (schema, table) | |
| columnstr = [] | |
| with open(self.file, "+br") as fp: | |
| fp.seek(self.tablepos[targettbl]) | |
| while True: | |
| line = fp.readline() | |
| if not line: | |
| break | |
| line = line.decode() | |
| columns = line[:-1].split(",") | |
| curtbl = (columns[COLUMN.SCHEMA.value], columns[COLUMN.TABLE.value]) | |
| if curtbl != targettbl: | |
| break | |
| columnstr.append(columns[COLUMN.COLUMN_NAME.value]) | |
| print(",".join(columnstr)) | |
| def list_directories_recursively(path): | |
| for root2, dirs, files in os.walk(path): | |
| root = root2 + "/" | |
| root = root2[len(path) + 1 :] | |
| # dirs = [x[len(path) :] for x in dirs2] | |
| # 現在のディレクトリ内のサブディレクトリをリストアップ | |
| for dir_name in files: | |
| print(f" Sub-directory: {root}{dir_name}") | |
| def main(): | |
| pretbl = None | |
| p = Parser("headers.csv") | |
| p.getColumns("S1", "T2") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment