Last active
December 24, 2023 08:07
-
-
Save snowyegret23/70f2a0be168f86a66c007a2551a06394 to your computer and use it in GitHub Desktop.
Scar of the Doll A Psycho-Horror Story about the Mystery of an Older Sister Day{x} 스크립트 작업 툴
This file contains 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 | |
# 사용법: | |
# 1. 현재 .py 파일 실행 후 종료 | |
# 2. 에러 메세지에 보이는 대로, 생성된 1_original_file 폴더 안에 대사 json 파일들 넣기 (UABEA를 통해 json으로 export 한 파일들) | |
# 3. .py 파일 재실행 | |
# 4. 이후 65, 66번 행의 주석처리에 따라 export/import모드 변경 가능 | |
# 5. import 작업 시에도 에러 메세지를 따라가면 됨. | |
def file_export(fn: str): | |
clean_fn = os.path.splitext(fn)[0] | |
os.makedirs("./2_exported_csv", exist_ok=True) | |
# fmt: off | |
with open(f"./1_original_file/{fn}", "r", encoding="utf-8") as f,\ | |
open(f"./2_exported_csv/{clean_fn}.csv", "w", encoding="utf-8-bom", newline="") as w: | |
data = json.load(f) | |
writer = csv.writer(w, quoting=csv.QUOTE_ALL) | |
writer.writerow(["No.", "RefIds", "Text"]) | |
n = 1 | |
for i, refids in enumerate(data["references"]["RefIds"]): | |
text = refids["data"].get("Text") | |
if text is not None: | |
writer.writerow([n, i, text["value"]]) | |
n += 1 | |
print(f"[export] {clean_fn}.json 작업 완료!") | |
def file_import(fn: str): | |
clean_fn = os.path.splitext(fn)[0] | |
os.makedirs("./3_result", exist_ok=True) | |
if not os.path.exists(f"./2_exported_csv/{clean_fn}.csv"): | |
print(f"{clean_fn}.csv 파일을 찾을 수 없습니다. 해당 파일의 작업을 건너뜁니다.") | |
return | |
# fmt:off | |
with open(f"./1_original_file/{clean_fn}.json", "r",encoding="utf-8") as f,\ | |
open(f"./2_exported_csv/{clean_fn}.csv", "r", encoding="utf-8-bom") as r,\ | |
open(f"./3_result/{clean_fn}.json", "w", encoding="utf-8") as w: | |
reader = csv.reader(r, quoting=csv.QUOTE_ALL) | |
orig_data = json.load(f) | |
next(reader) | |
for row in reader: | |
if len(row) > 3 and row[3].strip() != "": | |
orig_data["references"]["RefIds"][int(row[1])]["data"]["Text"]["value"] = row[3] | |
w.write(json.dumps(orig_data, ensure_ascii=False, indent=2)) | |
print(f"[import] {clean_fn}.json 작업 완료!") | |
if __name__ == "__main__": | |
try: | |
os.makedirs("./1_original_file", exist_ok=True) | |
filelist = [i for i in os.listdir("./1_original_file") if i.endswith(".json")] | |
if filelist == []: | |
print("1_original_file 폴더에 json 파일이 없습니다.") | |
print("원본 json 파일을 해당 폴더에 넣어주세요.") | |
input("아무 키나 눌러 종료하세요...") | |
exit(1) | |
for i in filelist: | |
file_export(i) | |
# file_import(i) | |
print("모든 작업이 완료되었습니다.") | |
input("아무 키나 눌러 종료하세요...") | |
except Exception as e: | |
print("에러가 발생했습니다.") | |
print(e) | |
input("아무 키나 눌러 종료하세요...") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment