首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python文本文档翻译比较

Python文本文档翻译比较
EN

Stack Overflow用户
提问于 2015-03-17 02:57:33
回答 2查看 138关注 0票数 0

很简单的问题。我试图创建一个“翻译比较”程序,它读取和比较两个文档,然后返回其他文档中没有的每个单词。这是一个初学者的课程,所以我试图避免使用模糊的内部方法,即使这意味着效率较低的代码。这就是我目前所拥有的..。

代码语言:javascript
复制
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  
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-17 03:09:09

你可以试试像这样的东西

代码语言:javascript
复制
#######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()

此外,使用它也是很好的做法。

代码语言:javascript
复制
with open("file1.txt", 'r') as f1:
        f1 =f1.read().split()

因为当您不使用with打开文件时,它将关闭该文件。Python非常擅长释放和管理内存,但确保释放它或调用它始终是一个好习惯。

代码语言:javascript
复制
file1.close()

当你完成的时候。

票数 1
EN

Stack Overflow用户

发布于 2015-03-17 03:12:41

假设您希望逐字比较,比如a b cb a c的比较会同时返回ab,那么ba (与原始代码中的None相反)

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29090571

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档