很简单的问题。我试图创建一个“翻译比较”程序,它读取和比较两个文档,然后返回其他文档中没有的每个单词。这是一个初学者的课程,所以我试图避免使用模糊的内部方法,即使这意味着效率较低的代码。这就是我目前所拥有的..。
def translation_comparison():
import re
file1 = open("Desktop/file1.txt","r")
file2 = open("Desktop/file2.txt","r")
text1 = file1.read()
text2 = file2.read()
text1 = re.findall(r'\w+',text1)
text2 = re.findall(r'\w+',text2)
for item in text2:
if item not in text1:
return item 发布于 2015-03-17 03:09:09
你可以试试像这样的东西
#######Test data
#file1.txt = this is a test
#file2.txt = this a test
#results#
#is
def translation_comparison():
with open("file1.txt", 'r') as f1:
f1 = f1.read().split()
with open("file2.txt", 'r') as f2:
f2 = f2.read().split()
for word in f1:
if word not in f2:
print(word)
translation_comparison()此外,使用它也是很好的做法。
with open("file1.txt", 'r') as f1:
f1 =f1.read().split()因为当您不使用with打开文件时,它将关闭该文件。Python非常擅长释放和管理内存,但确保释放它或调用它始终是一个好习惯。
file1.close()当你完成的时候。
发布于 2015-03-17 03:12:41
假设您希望逐字比较,比如a b c与b a c的比较会同时返回a和b,那么b和a (与原始代码中的None相反)
import string
import itertools
class FileExhausted(Exception): pass
def read_by_word(file):
def read_word():
while True:
l = file.read(1)
if l:
if l in string.whitespace:
break
yield l
else:
raise FileExhausted
while True:
this_word_gen = read_word()
try:
this_word = "".join(this_word_gen)
except FileExhausted:
break
else:
if this_word:
yield this_word
def translation_comparison():
with open("file1.txt") as file1, open("file2.txt") as file2:
words1 = read_by_word(file1)
words2 = read_by_word(file2)
for (word1, word2) in itertools.zip_longest(words1, words2, fillvalue=None):
if word1 != word2:
yield (word1, word2)https://stackoverflow.com/questions/29090571
复制相似问题