1

I have a program that saves a .txt log with multiple values and text. Those are time values can differ each time the program is ran, but their place in this .txt file is always the same. I need to grab those values and check if they are smaller than 6 seconds. I was trying to convert my .txt file to string, and then use:

x = string(filename[50:55])
float(x)

But it doesn't seem to work. How can I extract values form fixed place in .txt file and then convert them to float number?

//EDIT:

Photo of my log below, I want to check those values marked with blue line: Log

//EDIT2:

Photo of another log, how would I extract those percentages, those must be below 70 to pass. enter image description here

//EDIT3:Photo of the error I got and fragment of the code: enter image description here

with open(r'C:\Users\Time_Log.txt') as f:
    print(f)
    lines = f.readlines()
    print(r'Lines: ')
    print(lines)
    print(r'Type of lines:', type(lines))
# start at 1 to avoid the header
    for line in range(1, len(lines)):
        print(r'Type of line:', type(line))
        splits = line.split("|")
        print(r'Type of splits:', type(splits))
        t = splits[3].split(".")
        if t[0] < 6:
            print(r'Test completed. Startup time is shorter than 6 seconds')

1 Answers1

0

I mean with an example it would be much simpler, but assuming that your values have a fixed distance (idk space or tab) you'd split the string with that and look at the elements that you want to compare, so idk if the time is your 6th item you'd string.split(" ") and pick splitted_str[5]. You can do that further if you're time format follows a regular pattern idk hours:minutes:seconds and then do the math or you could even use packages like date or time to convert them to some time object which could potentially do some more useful comparison.

So the question is basically how well formatted your values are.

Edit: So given the example you could:

with open("filename.txt") as f:
    lines = f.readlines()
    # start at 1 to avoid the header
    for i in range(3, len(lines)):
        splits = lines[i].split("|")
        t = str(splits[3]).split(".")
        if int(t[0)] < 6:
             [do something with that line]
          
haxor789
  • 604
  • 6
  • 18
  • Yeah you'd pretty much do something similar, so check for the | then check for the space and use the column that you want to. Also if you can find reliable patterns in your data you can also give those a try: https://docs.python.org/3/howto/regex.html – haxor789 Jan 20 '22 at 13:32
  • I tried solution You gave, but yesterday I wasn't able to test it fully. Today I tried to implement it into my code and got an error regarding "lines" variable, as it seems that the script sees it as int and can't use split operation on it. Full photo of the error in first post. – Redal_Snake Jan 21 '22 at 11:30
  • According to the error message the error is for "line" not "lines", could you give the type() and value of what lines is? I mean line is apparently an int and as such has no split() method (which is for strings), but I'm not quite sure why it's an int, when the line is most likely a string. – haxor789 Jan 21 '22 at 12:18
  • I used print(type(lines)) and it seems that it sees lines as 'list'. – Redal_Snake Jan 21 '22 at 12:42
  • I mean that makes sense, it should be a list of the lines hence "lines". But the value of the elements in that list should be the strings that are written on that line. – haxor789 Jan 21 '22 at 13:01
  • And it is as You wrote. You can see it on the photo, cause I printed 'lines'. I also checked type of 'line' and it's string type. What should I do now? Because this seems strange, as both 'line' or 'lines' aren't int types. – Redal_Snake Jan 21 '22 at 13:18
  • You could post the part of the code you've used and the respective prints and error messages because that sounds indeed strange. Also maybe you need to skip more than just one line at the start, but there still should be no int. – haxor789 Jan 21 '22 at 13:30
  • Can't edit my comment. Sorry for misinformation. When I changed 'range(1, len(lines)) to path to whole file, type(lines) showed string, but now it shows int. – Redal_Snake Jan 21 '22 at 15:04
  • Ok that's embarrassing... Yes it's an int because it uses a range() in the for loop so it's not iterating the lines it's iterating an index so to get the line one would need to use llines[i]. I probably thought I skip the first line and then iterate the lines and then decides using indices would make that easier. Anyway that should yield a better result. – haxor789 Jan 21 '22 at 15:17
  • Ok, thanks for noticing that. I tried this solution and it seems that now problem lies with 'splits', because after I pasted Your edited code this error occurrs: t = str(splits[3].split(".") IndexError: list index out of range – Redal_Snake Jan 21 '22 at 17:40
  • Yeah that's probably because your document starts with a header so the first parameter of range should start later, so maybe range(3, len(...)) instead of range(1,...)? – haxor789 Jan 21 '22 at 18:50
  • Ok, tried it, seems that script moved on, becuase now it gives me an error regarding 'if" statement. 'if t[0] < 6 - TypeError: '<' not supported between instances of 'str' and 'int'. – Redal_Snake Jan 23 '22 at 18:53
  • Makes sense as we cast it to str() it's now no longer comparable to an int so you get the < is not supported error. Try using int(t[0]) instead. – haxor789 Jan 24 '22 at 07:49
  • 1
    Ok, I managed to solve that just by converting variable t[] that is a list first to 'str', and then to 'float'. Now the script goes through each value as intended and compares every value: Splits: ['C:106E308 ', ' AlgoProxy_ISRHandler ', ' 504581794 ', ' 4.220 ', ' 60.256 ', ' 0.307\n'] [' 4', '220 '] 4.220 4.22 Test completed. Startup time is shorter than 6 seconds But I still get an error: t = str(splits[3]).split(".") IndexError: list index out of range. But solved that, by using 'range(3, len(lines)-2), so it stops right after row with final value. Thank You for help. – Redal_Snake Jan 24 '22 at 08:01