简体   繁体   中英

Searching for float value in .txt file

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM