我使用了一小段非常简单的代码来读取正在编写的txt文件。以下是消息的外观:
2|00001A1|0009E47290
2|00001C7|AA200680
1|0000155|0087D35498900310第一位为1表示输出,2位表示输入
第二个字符串是消息的标识符
最后一个字符串是数据
我想按标识符过滤消息,例如:只打印标识符为0000155的消息,但我经验不是很丰富,有人能帮我吗?下面是代码目前的样子:
while True:
file = open("test.txt", "r")
x = file.read()
x = x.split("\n")
print(x[-2])发布于 2021-09-07 10:07:01
您可以像下面这样使用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
0087D45498900310https://stackoverflow.com/questions/69086112
复制相似问题