DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Andrew has posted 6 posts at DZone. View Full User Profile

Duplicate Finder In Text File

01.24.2009
| 4215 views |
  • submit to reddit
        // Searches for and prints duplicates within a text file.

import os

## Move into the directory where the database is.
os.chdir('folder where file is')

open_file = open('candidates.txt', 'r')
line = open_file.readline()
name = line.strip()

candidateDict = {}

def find_duplicate():
    for name in open_file:
        if candidateDict.has_key(name):
            candidateDict[name] += 1
        else:
            candidateDict[name] = 1

find_duplicate()

for name in candidateDict:
    if candidateDict[name] > 1:
        print name

open_file.close()