通过 python 从文件中获取不同的值

get distinct values from file via python

大家好! 我有一个文件,其中一列只有数字 (Paramkey.txt),我需要读取该文件并获取不同的值并将它们写入另一个 .txt 文件。有人可以帮忙写代码吗?到目前为止代码看起来像这样

infile=open("Paramkey.txt", "r") 
outfile=open("distkey.txt", "w") 
for line in infile: 
outfile.write(set(infile) 
else: 
pass 
infile.close() 
outfile.close()

我收到语法错误,与 else 语句有关。

  So i managed to finish the program and it's working:

    infile=open("Paramkey.txt", "r") 
    outfile=open("distkey.txt", "w")
    for line in infile:
    output=set(line.strip() for line in infile)
    print >> outfile, "distincts:", output 
    infile.close()
    outfile.close() 

and i just want to add i used the "print >>" instead of "print()" statement, because my python is version 2.7.x not 3.x