程式碼:
# 定義一個函式來處理檔案 def process_file(src_filename, unique_filename): seen = set() duplicates = set() with open(src_filename, 'r', encoding='utf-8') as file: for line in file: # 將讀取的行轉換為小寫,以避免大小寫差異導致的重複 normalized_line = line.strip().lower() if normalized_line in seen: duplicates.add(normalized_line) else: seen.add(normalized_line) # 列印重複的行 for dup in duplicates: print(f"Duplicate: {dup}") # 將不重複的行寫入新檔案 with open(unique_filename, 'w', encoding='utf-8') as file: for line in seen: file.write(line + '\n') # 呼叫函式,指定原始檔和新檔案的名稱 process_file('zhong.srt', 'unique.srt')