Python 文件中的字符串与集合中的字符串不匹配

Python 文件中的字符串与集合中的字符串不匹配,python,string,set,comparison,equals,Python,String,Set,Comparison,Equals,我有一个文件,每行有一个单词,还有一个单词集,我想把不相等的单词从一个叫做“out”的集中放到这个文件中。我的代码中有一部分: def createNextU(self): print "adding words to final file" if not os.path.exists(self.finalFile): open(self.finalFile, 'a').close fin = open(self.finalFile,"r") ou

我有一个文件,每行有一个单词,还有一个单词集,我想把不相等的单词从一个叫做“out”的集中放到这个文件中。我的代码中有一部分:

def createNextU(self):
    print "adding words to final file"
    if not os.path.exists(self.finalFile):
        open(self.finalFile, 'a').close
    fin = open(self.finalFile,"r")
    out = set()
    for line in self.lines_seen: #lines_seen is a set with words
        if line not in fin:
            out.add(line)
        else:
            print line
    fin.close()
    fout= open(self.finalFile,"a+")
    for line in out:
        fout.write(line)
但它只匹配一点点真正的对等词。我使用相同的单词词典,每次运行时它都会将重复的单词添加到文件中。我做错了什么??发生了什么事??我尝试使用“==”和“is”比较器,得到了相同的结果

编辑1:我正在处理大文件(finalFile),它不能在RAM中完全加载,所以我想我应该逐行读取文件

编辑2:发现指针有大问题:

def createNextU(self):
    print "adding words to final file"
    if not os.path.exists(self.finalFile):
        open(self.finalFile, 'a').close
    out = set()
    out.clear()
    with open(self.finalFile,"r") as fin:
        for word in self.lines_seen:
            fin.seek(0, 0)'''with this line speed down to 40 lines/second,without it dont work'''
            if word in fin:
                self.totalmatches = self.totalmatches+1
            else:
                out.add(word)
                self.totalLines=self.totalLines+1


    fout= open(self.finalFile,"a+")
    for line in out:
        fout.write(line)
如果我在打开文件之前先将所看到的行放入bucle,我会为所看到的行中的每一行打开文件,但速度仅会提高到30k行/秒。使用set()时,我最多每秒有200k行,因此我想我将按部分加载文件,并使用set进行比较。有更好的解决办法吗


编辑3:完成

fin
是一个文件句柄,所以如果行不在fin中,则无法将其与
进行比较。首先需要阅读内容

with open(self.finalFile, "r") as fh:
    fin = fh.read().splitlines()   # fin is now a list of words from finalFile

for line in self.lines_seen: #lines_seen is a set with words
    if line not in fin:
        out.add(line)
    else:
        print line
# remove fin.close()
编辑:

由于所看到的
lines\u
是一个集合,请尝试使用
finalFile
中的单词创建一个新集合,然后区分集合

file_set = set()

with open(self.finalFile, "r") as fh:
    for f_line in fh:
        new_set.add(f_line.strip())

# This will give you all the words in finalFile that are not in lines_seen.
print new_set.difference(self.lines_seen)

您的比较可能不起作用,因为从文件中读取的行的末尾将有一个换行符,因此您正在将“word\n”与“word”进行比较。使用“rstrip”将有助于删除尾随的换行符:

>>> foo = 'hello\n'
>>> foo
'hello\n'
>>> foo.rstrip()
'hello'
我还将迭代该文件,而不是迭代包含要检查的单词的变量。如果我已经理解了您的代码,那么您希望编写self.lines\u self.finalFile中的任何内容,如果它还没有在self.lines\u self.finalFile中的话。如果您使用“If line not in fin”(如果行不在fin中),这将不会像您预期的那样工作。例如,如果您的文件包含:

lineone
linetwo
linethree
如果设置的行是无序的,则返回“linethree”,然后返回“linetwo”,那么以下内容将与“linethree”匹配,但与“linetwo”不匹配,因为文件对象已经读取了它:

with open(self.finalFile,"r" as fin:
    for line in self.lines_seen:
        if line not in fin:
            print line

相反,考虑使用计数器:

from collections import Counter
linecount = Counter()
# using 'with' means you don't have to worry about closing it once the block ends
with open(self.finalFile,"r") as fin:
    for line in fin:
        line = line.rstrip() # remove the right-most whitespace/newline
        linecount[line] += 1
for word in self.lines_seen:
    if word not in linecount:
        out.add(word)

你好首先感谢大家的回答。如果我有一个巨大的文件要打开,比如100GB,它不能在内存(RAM)中加载,使用这个计数器会得到一个内存错误,对吗?嗨,我尝试了这个,但出现了一些问题,控制台输出被冻结,idk为什么,但非常感谢!!Hi@user3754301可能会尝试使用
finalFile
创建一个新的集合,并将其与您的单词集合进行区分。我将文件按小部分加载到RAM可以容纳的集合中,有时使用“差异”来最终获得唯一的单词。使用此解决方案,我可以获得非常高的性能。thnx!!