大éã«ããã¨ããã°ã©ã ä¸è¦§ãä½ããã¨ã§ãã大å¤ã§ãã ã¡ãã£ã¨ã§ã楽ãããããã«ããã°ã©ã ä¸è¦§ãã¨ã¯ã»ã«ã«ããããã°ã©ã ãä½ãã¾ãã
äºåä½æ¥
1 . Tree: ãã£ã¬ã¯ããªã®ããªã¼æ§é ã表示ããããã®ã³ãã³ãã©ã¤ã³ãã¼ã«ã§ããã¿ã¼ããã«ã§ tree
ã³ãã³ããå®è¡ããã¨ãã«ã¬ã³ããã£ã¬ã¯ããªä»¥ä¸ã®ãã£ã¬ã¯ããªã¨ãã¡ã¤ã«ãé層æ§é ã§è¡¨ç¤ºããã¾ãã
Macã«ã¯ããã©ã«ãã§ã¤ã³ã¹ãã¼ã«ããã¦ããªããã¨ãããã¾ãããHomebrewãªã©ã®ããã±ã¼ã¸ããã¼ã¸ã£ã¼ã使ã£ã¦ã¤ã³ã¹ãã¼ã«ãããã¨ãã§ãã¾ãã
brew install tree
ã¤ã³ã¹ãã¼ã«å¾ã¯ãtree
ã³ãã³ãã§ãã£ã¬ã¯ããªæ§é ãç°¡åã«è¡¨ç¤ºã§ãã¾ãã
2 . openpyxl: openpyxl
㯠pythonã§ã¨ã¯ã»ã«ãã¡ã¤ã«ãä½æããã¨ãã«ä½¿ç¨ãã¾ã
pip install openpyxl # pipãå ¥ã£ã¦ããªãå ´å python3 -m pip install openpyxl
åºå
treeã³ãã³ãã§åºåããã ãã§ã
cd Projectãã©ã«ã tree > tree_output.txt
åºåçµæãã¨ã¯ã»ã«ã«ãã
import os from openpyxl import Workbook def parse_tree_output(file_path): with open(file_path, 'r', encoding='utf-8') as f: lines = f.readlines() parsed_lines = [] for line in lines: # Remove tree symbols and leading spaces cleaned_line = line.lstrip().replace("âââ", "").replace("âââ", "") # cleaned_lineã«âãå«ã¾ãã¦ããæ°ãåå¾ãã pipe_count = cleaned_line.count("â") indent_level = pipe_count + 1 # cleaned_lineããâã¨å ¨è§ã¹ãã¼ã¹ã¨åè§ã¹ãã¼ã¹ãåé¤ãã cleaned_line = cleaned_line.replace("â", "").replace("ã", "").replace(" ", "") # Determine indent level #indent_level = len(line) - len(line.lstrip()) #debug log #print(f"Indent Level: {indent_level}, Line: {cleaned_line}") parsed_lines.append((cleaned_line.strip(), indent_level)) return parsed_lines def write_to_excel(data, excel_file): wb = Workbook() ws = wb.active ws.title = 'Tree Output' for line, indent_level in data: # Write to Excel adjusting indent level ws.cell(row=ws.max_row + 1, column=indent_level + 1, value=line) wb.save(excel_file) print(f"Excel file '{excel_file}' created successfully.") if __name__ == "__main__": tree_file_path = 'tree_output.txt' # Replace with your Tree command output file path excel_file_path = 'tree_output.xlsx' # Output Excel file path parsed_data = parse_tree_output(tree_file_path) write_to_excel(parsed_data, excel_file_path)
ãã®ã¹ã¯ãªããã§ã¯ãparse_tree_output é¢æ°ã§ tree_output.txt ãã¡ã¤ã«ãèªã¿åããåè¡ã®ã¤ã³ãã³ãã¬ãã«ã¨ãã¹ãæ½åºãã¾ãã ãã®å¾ãcreate_excel_from_tree_output é¢æ°ã§ãåå¾ãããã¼ã¿ãæ°ããã¨ã¯ã»ã«ãã¡ã¤ã«ã«æ¸ãè¾¼ã¿ã¾ããã¨ã¯ã»ã«ãã¡ã¤ã«ã¯ç¾å¨ã®ãã£ã¬ã¯ããªã« tree_output.xlsx ã¨ãã¦ä¿åããã¾ãã
ä¸æ ¼å¥½ã§ããå å·¥ãããããªãã¾ãã