简体   繁体   中英

Comparing values in txt file

Ok, so I have a txt file that contains messages from electrical units.

That's the message form:

ID  time(ms)     EU ID      Val1   Val2
1      0          0x100      0       0 

here's few lines for the txt file:

1 0 0x200 0 0
2 0 0x100 0 0
3 0 0x400 0 0
4 0 0x800 0 100
5 5 0x200 0 11

I want to validate that for example, unit 0x100 sends a message every 50 milliseconds.

So iterating through this txt file I did took the next steps:

def detect_timing_anomalies():
    with open('100.txt') as file:
        times_anomalies = file.readlines()
        msgs = [msg for msg in times_anomalies]
        msgs = list(map(lambda msg: list(msg.split(" ")), msgs))
        for i in range(0, len(msgs)):
            list_0x100 = []
            if msgs[i][2] == "0x100":
                print(msgs[i])

Now I'm facing a problem figuring out how to actually compare between each msg that was received by the unit 0x100 and see if the time difference between each message is exactly 50 milliseconds and if not to save the ID of that msg(line) in a list of ids of anomalies.

Here is an example:

def detect_timing_anomalies():
    with open('file.txt') as file:
        lines = file.readlines()
        messages = [msg.rstrip().split(' ') for msg in lines]

        expected_time_intervals = {'0x100': 50, '0x200': 50, '0x400': 50, '0x800': 50}
        previous_timings = {}
        anomalies = []

        for message in messages:
            message_id = int(message[0])
            message_time = int(message[1])
            message_unit = message[2]

            previous_timing = previous_timings.get(message_unit)
            expected_time_interval = expected_time_intervals.get(message_unit)

            if previous_timing != None and message_time - previous_timing != expected_time_interval:
                anomalies.append(message_id)
            
            previous_timings[message_unit] = message_time

    return anomalies

A trivial way to start I think should be as fast and easy as possible:

times = [msg.split()[1] for msg in open('100.txt') if '0x100' in msg]
import numpy as np
d = np.array(times).diff()
print(d[d!=50])

Then refine from there. GL!

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