import json import re def sort_pairs_by_ai_image(input_path, output_path): # '..._<숫자1>_<숫자2>.jpg' 패턴: prefix는 숫자1 전까지 전부 pattern = re.compile(r"(.+)_(\d+)_(\d+)\.jpg$") entries = [] # 1. 읽어서 (prefix, num1, num2, entry)로 저장 with open(input_path, 'r', encoding='utf-8') as infile: for line in infile: entry = json.loads(line) ai_img = entry.get('ai_image', '') m = pattern.match(ai_img) if m: prefix, num1, num2 = m.groups() entries.append((prefix, int(num1), int(num2), entry)) else: # 패턴 안 맞으면 맨 뒤로 entries.append((ai_img, float('inf'), float('inf'), entry)) # 2. (prefix, num1, num2) 순서로 정렬 entries.sort(key=lambda x: (x[0], x[1], x[2])) # 3. new_pairs.jsonl에 기록 with open(output_path, 'w', encoding='utf-8') as outfile: for _, _, _, entry in entries: outfile.write(json.dumps(entry, ensure_ascii=False) + '\n') if __name__ == "__main__": sort_pairs_by_ai_image("pairs.jsonl", "new_pairs.jsonl") print("✅ new_pairs.jsonl 생성 완료했어! 😉")