6

I have the following code to replace just one set of value from 26th line of 150 lines data. The problem is with the nested for loop. From the second iteration onwards, the inner loop isn't executing. Instead the loop skips to the last line

n= int(input("Enter the Number of values: "))
arr = []
print ('Enter the Fz values (in Newton): ')
for _ in range(n):
    x=(input())
    arr.append(x)
print ('Array: ', arr)

os.chdir ("D:\Work\python")
f = open("datanew.INP", "r+")
f1 = open("data.INP", "w+")

for i in range(len(arr)):
    str2 = arr[i]
    for j,line in enumerate(f):
        if j == 25 :
            new = line.split()
            linenew = line.replace(new[1],str2)
            print (linenew)
            f1.write(linenew)
        else:
            f1.write(line)
    print(arr[i], 'is replaced')
f.close()
f1.close()
3
  • This code doesn't even get to the loops when I run it, returning a IndexError: list index out of range. Please post code that works.
    – GreenMatt
    Commented Oct 18, 2018 at 13:49
  • I am sorry.. But this is a fragment of the code.. Also it does works when I try
    – Madhurina
    Commented Oct 18, 2018 at 13:58
  • Please have a look at this: stackoverflow.com/help/mcve
    – GreenMatt
    Commented Oct 18, 2018 at 15:40

1 Answer 1

4

The issue is that your code is looping over a file. On the first pass through, the end of file is reached. After that, there is no data left in the file to read, so the next loop has nothing to iterate over.

Instead, you might try reading over the whole file and storing the data in a list, then looping over that list. (Alternatively, you could eliminate the loops and access the 26th item directly.)

Here is some simple code to read from one file, replace the 26th line, and write to another file:

f = open("data.INP", "r")       # Note that for simple reading you don't need the '+' added to the 'r'
the_data = f.readlines()
f.close()
the_data[25] = 'new data\n'     # Remember that Python uses 0 indexing, thus the 25

f1 = open("updated_data.out", "w")  # Assuming you want to write a new file, leave off the '+' here, as that indicates that you want to append to an existing file
for l in the_data:
    f1.write(l)
f1.close()
3
  • from f to f1, a copy is happening. So the source file data is not disturbed.
    – Madhurina
    Commented Oct 18, 2018 at 14:01
  • Actually I am able to access the 26th line directly but. I need to replace values and send back the file to another processing. So I had to use this way to replace on value and store the file.. Is thr anyothr way i can do this??
    – Madhurina
    Commented Oct 18, 2018 at 14:03
  • @Madhurina: (Reposted comment, tagging you this time, in case you missed the earlier message) Please see the code I posted on how you can read the file all at once, directly modify the 26th member, and the write the data to a new file.
    – GreenMatt
    Commented Oct 18, 2018 at 15:34

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.