流过滤canbus消息使用python



我正在使用一段非常简单的代码来读取正在编写的txt文件。这些是消息的样子:

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

第一个位是1表示输出,2表示输入

第二个字符串是消息

的标识符最后一个字符串是data

我想通过标识符过滤消息,例如:只打印标识符0000155的消息,但我不是很有经验,有人可以帮助吗?这是代码现在的样子:

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

您可以像下面这样使用split:

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

输出:

1
0000155
0087D45498900310

最新更新