KZwin
KZwin

Reputation: 21

stream filtering canbus messages using python

I'm using a very simple bit of code for reading a txt file while its being written. These are what the messages look like:

2|00001A1|0009E47290      
2|00001C7|AA200680        
1|0000155|0087D35498900310

The first bit is 1 for output and 2 for input

The second string is the identifier of the message

The last string is the data

I'd like to filter the messages by identifier, for example: only print messages with identifier 0000155, but I'm not very experienced, anybody that can help? This is what the code looks like at the moment:

while True:
file = open("test.txt", "r")
x = file.read()
x = x.split("\n")
print(x[-2])

Upvotes: 1

Views: 269

Answers (1)

I'mahdi
I'mahdi

Reputation: 24049

you can use split like below:

# x = "1|0000155|0087D45498900310"
first_bit, second_sttring, last_string = tuple(x.split('|'))

print(first_bit)
print(second_sttring)
print(last_string)

Output:

1
0000155
0087D45498900310

Upvotes: 1

Related Questions