26A09c

Young-Kyoo Kim·2026년 4월 9일
import json

file_path = 'data.json'
output_field = 'name' # 뽑아내고 싶은 필드명
results = []

# 파일을 한 줄씩 읽어서 처리
with open(file_path, 'r', encoding='utf-8') as f:
    for line_number, line in enumerate(f, 1):
        line = line.strip()
        if not line: continue  # 빈 줄은 건너뜀
        
        try:
            # 한 줄씩 JSON 객체로 변환
            data = json.loads(line)
            # 특정 필드 값 추출
            if output_field in data:
                results.append(data[output_field])
        except json.JSONDecodeError as e:
            print(f"{line_number}행에서 오류 발생: {e}")

# 결과 출력 (유니코드 변환 확인)
for val in results:
    print(val)

# 필요한 경우 파일로 저장
with open('extracted_data.txt', 'w', encoding='utf-8') as f:
    for val in results:
        f.write(str(val) + '\n')

0개의 댓글