トップ 差分 一覧 ソース 検索 ヘルプ PDF RSS ログイン

common.py

common.py(349) (for Python 2)


# copyleft 2011-05-08 sugiura@nagoya-u.jp
# 二つのファイルに共通する単語を選び出す。
# 前提:二つのファイルには、一行一単語で並んでいること

import time

f1 = open(input("Put in the first file name with double-quote marks: "))
firstFile = f1.readlines()
linesA = len(firstFile)
print linesA, "items are included.\n"


f2 = open(input("Put in the second file name with double-quote marks: "))
secondFile = f2.readlines()
linesB = len(secondFile)
print linesB, "items are included.\n"

f3 = open('common.txt', 'w')

for x in range(linesA):
        are = firstFile[x]
        for y in range(linesB):
                kore = secondFile[y]
                if are == kore:
                        f3.write(are)

f3 = open('common.txt', 'r')
thirdFile = f3.readlines()
linesC = len(thirdFile)
print "============================="
print linesC, "items are common."
print "============================="
time.sleep(2)

for z in range(linesC):
        print "(",z+1,")", thirdFile[z]

print "=============================\n"
print "That's it! Open the file called common.txt."

time.sleep(3)