두 개의 텍스트 파일을 줄 단위로 비교하여 다른 부분을 출력하는 코드
file1_path = 'energy_op_code.txt'
file2_path = 'energy_op_code_k.txt'
with open(file1_path, 'r', encoding='utf-8') as f1, open(file2_path, 'r', encoding='utf-8') as f2:
lines1 = f1.readlines()
lines2 = f2.readlines()
max_len = max(len(lines1), len(lines2))
for i in range(max_len):
line1 = lines1[i].strip() if i < len(lines1) else ''
line2 = lines2[i].strip() if i < len(lines2) else ''
if line1 != line2:
print(f"Line {i+1} is different:")
print(f" file1: {line1}")
print(f" file2: {line2}")
두 개의 csv 파일에서 특정 필드 비교 후 다른 항목 찾기
import csv
file1_path = 'energy_op_code.txt'
file2_path = 'energy_op_code_k.txt'
def get_3rd_field_set(filepath):
result = set()
with open(filepath, 'r', encoding='utf-8') as f:
reader = csv.reader(f)
for row in reader:
if len(row) > 2:
value = row[2].strip()
if value:
result.add(value)
return result
set1 = get_3rd_field_set(file1_path)
set2 = get_3rd_field_set(file2_path)
only_in_1 = set1 - set2
only_in_2 = set2 - set1
print("Values only in file1 (column 3):")
for v in sorted(only_in_1):
print(v)
print("\nValues only in file2 (column 3):")
for v in sorted(only_in_2):
print(v)
중복 포함, 등장 횟수까지 보여주는 코드 (Counter 사용)
import csv
from collections import Counter
file1_path = 'energy_op_code.txt'
file2_path = 'energy_op_code_k.txt'
def get_3rd_field_list(filepath):
result = []
with open(filepath, 'r', encoding='utf-8') as f:
reader = csv.reader(f)
for row in reader:
if len(row) > 2:
value = row[2].strip()
if value:
result.append(value)
return result
list1 = get_3rd_field_list(file1_path)
list2 = get_3rd_field_list(file2_path)
counter1 = Counter(list1)
counter2 = Counter(list2)
only_in_2 = [val for val in list2 if val not in set(list1)]
result = Counter(only_in_2)
print("Values only in file2 (column 3) with counts:")
for value, count in result.items():
print(f"{value}: {count}")