I am trying to find and replace the values in the text file
Question: I have a text file containing values (e.g.0.2803739 0.280314). I would like to
replace the values in the text file in such a way that highest value will be
replaced by lowest value and so on. e.g. If text file contains values from 1
to 10 then value 10 should be replaced by value 1, value 9 should be replaced
by 2, value 8 should be replaced by 3 and so on. In the following script the
3rd "for loop" is getting ignored.

    
    
    import fileinput
    
    text_file = r"E:\Jagu\test123.txt"
    f1 = open(text_file, 'r')
    
    unique_v = set(f1.read().split())
    a = list(unique_v)
    
    new_list1= list(a)
    new_list1.sort(reverse = True )
    
    new_list2 = new_list1
    new_list2.sort(reverse = False )
    
    l = 0
    m = len (new_list2)
    m = m-1
    
    f2 = open(text_file + ".tmp", 'w')
    
    for j in new_list1: 
        c = new_list1 [l]
        l = l + 1
        for k in new_list2:
            d = new_list2[m]
            m = m - 1
            for line in f1:
                f2.write(line.replace(c,d))
                print "replaced : " + str(c)  + " with : " + str(d)
    f1.close()
    f2.close()
    

Hope the explanation is helpful to understand my issue. I am a beginner in
Python programming. Any help would be appreciated.


Answer: Issue 1:

    
    
    new_list2 = new_list1
    

This will make `new_list2` and `new_list1` point to same list. You need

    
    
    new_list2 = list(a)
    

or

    
    
    new_list2 = new_list[:]
    

Issue 2:

You cannot do

    
    
    for line in f1:
    

after you have `read()` from `f1`.

Do

    
    
    f1.seek(0)
    l = f1.readlines()
    for line in f1:
    

