Created
May 12, 2025 03:08
-
-
Save snowyegret23/5e1d029618e4b85e770238a6b25ce952 to your computer and use it in GitHub Desktop.
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
| import json | |
| import csv | |
| import os | |
| def extract_text_with_paths(data, current_path=None): | |
| if current_path is None: | |
| current_path = [] | |
| result = [] | |
| if data is None: | |
| return result | |
| if isinstance(data, dict): | |
| for key, value in data.items(): | |
| new_path = current_path + [key] | |
| if isinstance(value, str): | |
| result.append((new_path, value)) | |
| else: | |
| result.extend(extract_text_with_paths(value, new_path)) | |
| elif isinstance(data, list): | |
| for i, item in enumerate(data): | |
| new_path = current_path + [i] | |
| if isinstance(item, str): | |
| result.append((new_path, item)) | |
| else: | |
| result.extend(extract_text_with_paths(item, new_path)) | |
| return result | |
| def export_json_to_csv(json_file_path, csv_file_path): | |
| with open(json_file_path, "r", encoding="utf-8") as f: | |
| data = json.load(f) | |
| text_with_paths = extract_text_with_paths(data) | |
| with open(csv_file_path, "w", newline="", encoding="utf-8") as f: | |
| writer = csv.writer(f, quoting=csv.QUOTE_ALL) | |
| writer.writerow(["Path", "Src", "Dst"]) | |
| for path, value in text_with_paths: | |
| path_str = "$$".join(("lstIndex" + str(p)) if isinstance(p, int) else str(p) for p in path) | |
| writer.writerow([path_str, value, ""]) | |
| print(f"JSON이 CSV로 성공적으로 내보내졌습니다: {csv_file_path}") | |
| return len(text_with_paths) | |
| def import_csv_to_json(csv_file_path, json_output_path, json_template_path): | |
| with open(json_template_path, "r", encoding="utf-8") as f: | |
| result = json.load(f) | |
| updated_count = 0 | |
| with open(csv_file_path, "r", encoding="utf-8") as f: | |
| reader = csv.reader(f) | |
| next(reader) | |
| for row in reader: | |
| if len(row) > 2 and row[2] != "": | |
| path_str = row[0] | |
| src = row[1] | |
| dst = row[2] | |
| path = [] | |
| for part in path_str.split("$$"): | |
| if part.startswith("lstIndex"): | |
| path.append(int(part.replace("lstIndex", ""))) | |
| else: | |
| path.append(part) | |
| try: | |
| current = result | |
| for key in path[:-1]: | |
| current = current[key] | |
| current[path[-1]] = dst | |
| updated_count += 1 | |
| except (KeyError, IndexError, TypeError): | |
| print(f"경로를 찾을 수 없어 무시됨: {path_str}") | |
| # 결과 저장 | |
| with open(json_output_path, "w", encoding="utf-8") as f: | |
| json.dump(result, f, ensure_ascii=False, indent=2) | |
| print(f"CSV가 JSON으로 성공적으로 가져와졌습니다: {json_output_path}") | |
| print(f"총 {updated_count}개의 텍스트 항목이 업데이트되었습니다.") | |
| return updated_count | |
| if __name__ == "__main__": | |
| json_file_path = "./localize/4d60762622c8739989661a9419f70fc78f9b7885.json" | |
| csv_file_path = "./localize/4d60762622c8739989661a9419f70fc78f9b7885_7merged.csv" | |
| json_output_path = "./localize/4d60762622c8739989661a9419f70fc78f9b7885_modified.json" | |
| # item_count = export_json_to_csv(json_file_path, csv_file_path) | |
| # print(f"총 {item_count}개의 텍스트 항목을 내보냈습니다.") | |
| import_csv_to_json(csv_file_path, json_output_path, json_file_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment