Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过python从文件中获取不同的值_Python_File_Distinct - Fatal编程技术网

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

通过python从文件中获取不同的值,python,file,distinct,Python,File,Distinct,各位!! 我有一个文件包含一列,其中只有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.clos

各位!! 我有一个文件包含一列,其中只有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 denbdar!根据h4z3,您能否提供一个可复制的示例?您可能需要阅读有关什么是可复制示例的信息。因此,fsr我的代码看起来像:infle=openParamkey.txt,r outfile=opendistkey.txt,w代表infle:outfile.writesteinfile中的行,否则:pass infle.close outfile.close,但当我运行程序时,它会给我语法错误:第5行的语法无效,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