将列表与文本文档进行比较

时间:2013-08-22 22:08:05

标签: python python-2.7

所以我在我的代码中生成一个列表,现在我想根据现有文档检查它,看看在创建新文档之前有什么不同。

这是我的尝试:

  diff = ""
    if File2_Loc :
        File2 = open( File2_Loc , 'r' )
        for line in R_List :
            if line in File2 :
                pass
            else :
                diff += ( line.strip() + " not found in old file.\n" )
        for line in File2 :
            if line == "***** Differences founds : *****\n" :
                print( "Compared!")
                break
            if line in R_List :
                pass
            else :
                diff += ( line.strip() + " not found in new file.\n" )
    else :
        print( "No file inputted:" )
    for line in R_List :
        File1.write( line )
    File1.write( "***** Differences founds : *****\n" )
    if diff :
        File1.write( diff )
    else :
        File1.write( "None.\n" )

这里的问题是,在File2中找不到R_List中的每一行,即使它们中的100%应该是。

我已经找到了一个解决方案,但我没有看到任何解决我的问题或解决我的问题的方法。

1 个答案:

答案 0 :(得分:2)

这是因为该文件只读一次。如果你在它上面调用“in”,它就不会再次迭代(它从当前位置“读取”,即文件的结尾)。因此解决方案是使用File2.readlines()将所有文件读入内存并尝试“in”: - )

    File2 = open( File2_Loc , 'r' )
    lines2 = File2.readlines()  # Define the lines here
    File2.close()               # This is also a good idea
    for line in R_List :
        if line in lines2 :     # Iterate over lines instead of file
            pass
        else :
            diff += ( line.strip() + " not found in old file.\n" )
    for line in lines2 :        # Iterate over lines instead of file
        if line == "***** Differences founds : *****\n" :
            print( "Compared!")
            break
        if line in R_List :
            pass
        else :
            diff += ( line.strip() + " not found in new file.\n" )

解决方案2: 该解决方案使用集合和运算符“ - ”对它们进行分离:

    File2 = open( File2_Loc , 'r' )
    lines2 = File2.readlines()  # Define the lines here
    File2.close()               # This is also a good idea
    not_in_file2 = list(set(R_list) - set(lines2))
    not_in_rlist = list(set(lines2) - set(R_list))
    # Finish the diff output accordingly.