[Tutor] Python text file read/compare

samira khoda samiraeastcoast at gmail.com
Thu Sep 29 14:09:28 EDT 2022


Hello

I am very new to python programming. I actually just finished my course
provided by my employer and now practicing on some data processing and I
need some direction to solve the issue I have. Any help would be
appreciated.

I am trying to create a loop that reads the file line by line and compares
the last timestamp to the current one in my data to find the line number
where to split the files. Then do another loop that writes lines to a new
file based on those line numbers.

Below is the snapshot of the data. Basically I need to find where the
timestamps jump down to zero or the lowest number so I can split the file.
For your information the timestamps are in milliseconds on the last column.

[image: image.png]

*And below is the code I wrote but nothing came out of it. Not even a new
text file(s). I am sure there is definitely something wrong with this. I
have a very hard time to figure this out.*

# op stands for output
# opf stands for output file

with open('hfx-test-.txt') as kyk:
    op=''
    start=0
    count=1
    for x in kyk.read().split("\n"):
        if(x=='0'):
            if (start==1):
                with open(str(count)+ '.txt', 'w') as opf:
                    opf.write(op)
                    opf.close()
                    op=''
                    count+=1

        else:
            start=1

    else:
        if(op==''):

          op = x

        else:
            op= op+ '\n' + x

kyk.close()


More information about the Tutor mailing list