1

I'm working on an assignment for school where I have a text file: data.txt which looks like this:(instead of 'name' there are actual names I just replaced them here)

10001-31021 'name'    2015.12.30.   524432

10001-31121 'name'  2016.03.21. 765432

10012-34321 'name'  2016.02.20. 231231

10201-11021 'name'  2016.01.10. 2310456

And I have an update.txt which looks like this:

2016.03.22.
10001-31021 'name'  +20000

10012-34321 'name'  +35432

10012-34321 'name'  -10000

10120-00123 'name'  +120334

10001-31021 'name'  +5000

10210-41011 'name'  -6000

10201-11021 'name'  +100210

12345-32100 'name'  +123456

And I have to make a newdata.txt file according to the changes to the last column that update.txt includes.

This is my code so far:

adat = open("data.txt", "r")
newdata = open("newdata.txt", "w")
update = open("update", "r")

date = update.readline().decode("utf-8-sig").encode("utf-8").splitlines()
num_lines = sum(1 for line in open('update'))
elsociklus = 0
masodikciklus = 0

for num_lines in update:
    updateData = re.search("(.{11}\t)(\D+\t)([+-]\d+)", num_lines)
    elsociklus = elsociklus + 1
    print("elsociklus: " + str(elsociklus))
    for j in adat:
        data = re.search("(.{11}\t)(\D+\t)(\d{4}\.\d{2}\.\d{2}\.\t)(\d+)", j)
        masodikciklus = masodikciklus + 1
        print("masodikciklus: " + str(masodikciklus))
        if data != None:
            if updateData.group(1) == data.group(1):
                print("regi: " + data.group(0))
                print("update: " + updateData.group(0))
                print("uj: " + data.group(1) + data.group(2) + date[0] + "\t" + str(int(data.group(4)) + int(updateData.group(3))))
                newdata.write(data.group(1) + data.group(2) + date[0] + "\t" + str(int(data.group(4)) + int(updateData.group(3))))
                newdata.write("\n")
            else:
                print("nincs valtozas: " + data.group(0))

adat.close()
newdata.close()
update.close()

My problem is with the nested loop. I just can't figure it out why it isn't entering the inner loop for the second time. It works perfectly on the first iteration but when entering the 2nd one in the outer loop it just ignores the inner loop.

Thank you in advance for your help.

3
  • 2
    Have you tested the values of j and adat? A loop won't run if the loop counter (j in this case) is outside of the range of the list. My guess is that you are running through adat to the end the first time through the outside loop and the inner loop is detecting that is already at the end of the file for the inside loop. The solution should be to close and reopen the file, or set the file pointer back to the beginning. Commented Apr 5, 2017 at 14:12
  • I see that in adat you just open the file and store the returned value. I dont see adat.read() or adat.readlines() anywhere in your code to read the contents of data.txt. So based on what are you iterating? Commented Apr 5, 2017 at 14:23
  • What's the point of setting num_lines before the loop? The value is never used, resulting in unnecessary I/O to read the entire file. Commented Apr 5, 2017 at 21:00

1 Answer 1

1

Thanks to codingCat for the answer. I fixed the problem by returning my file pointer to the beginning of my file in the inner loop

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.