首页 > 解决方案 > 如何根据字符串在两行之间提取文本文件中的行

问题描述

我有以下示例文本文件(格式如下所示)。我想在文本文件中提取“received msg from tag”和“geographic ....”行,这是我感兴趣的开始和结束。从 tag 收到的 msg : : {"tagId" :“ca026847cfca416e”,“锚”:[{“RSSI”:-70,“ID”:“P ID 00021f”},{“RSSI”:-76,“ID”:“P ID 00021d”},{“ RSSI”:-81,“ID”:“P ID 00010d”}],“tagData”:{“batteryLevel”:2.862}} 有效载荷:{“tagId”:“ca026847cfca416e”,“geographic”:{“techno”: “GPS”,“lng”:-3.707135511213621,“lat”:40.40968001418976,“alt”:0,“hdop”:3},}]} 我想根据收到的来自标签的消息和带有地理位置的响应来提取下面的这些行。但有时文本文件的格式并不总是正确且未给出位置,但收到的响应是 Payload : {"tagId" : "ca026847cfca416e","tagSensorData" : [{"type" : "batteryLevel","value" : "2.856"}]} 如何提取文本文件中关于从标签接收到的开始消息的行 }]} 我想根据收到的来自标签的消息和带有地理位置的响应来提取下面的这些行。但有时文本文件的格式并不总是正确且未给出位置,但收到的响应是 Payload : {"tagId" : "ca026847cfca416e","tagSensorData" : [{"type" : "batteryLevel","value" : "2.856"}]} 如何提取文本文件中关于从标签接收到的开始消息的行
和地理位置。

我编写了以下代码,但它没有找到文本文件的行。

import re

message = "received msg from tag"  # Texte  rechercher
geographic = "geographic"
isWithin = False
with open("result_Trace_LocEngine.txt", "r") as file, open("result_rssi_LocEngine.txt", "w") as resfile:
    isWithin = False
    for line in file:
        if geographic in line:
#            isWithin = False
            if isWithin == True:
                resfile.write(line)
        if message in line:
            isWithin = True
            resfile.write(line)
            
# print(line)   
file.close()
resfile.close()

result below 2021-10-07 17:12:53,551 - web2py.app.locationEngine - INFO - received msg from tag : : {"tagId": "ca026847cfca416e", "anchor": [{"RSSI": -70, "ID": "P ID 00021f"}, {"RSSI": -76, "ID": "P ID 00021d"}, {"RSSI": -81, "ID": "P ID 00010d"}], "tagData": {"batteryLevel": 2.862}} 2021-10-07 17:13:53,532 - web2py.app.locationEngine - INFO - received msg from tag : : {"tagId": "ca026847cfca416e", "anchor": [{"RSSI": -69, "ID": "P ID 00021f"}, {"RSSI": -79, "ID": "P ID 00021d"}, {"RSSI": -86, "ID": "P ID 00010d"}], "tagData": {"batteryLevel": 2.856}} 2021-10-07 17:14:53,643 - web2py.app.locationEngine - INFO - received msg from tag : : {"tagId": "ca026847cfca416e", "anchor": [{"RSSI": -64, "ID": "P ID 00021f"}, {"RSSI": -82, "ID": "P ID 00010d"}, {"RSSI": -82, "ID": "P ID 00021d"}, {"RSSI": -90, "ID": "P ID 00010a"}], "tagData": {"batteryLevel": 2.862}} 2021-10-07 17:14:53,666 - web2py.app.locationEngine - INFO - Payload : {"tagId" : "ca026847cfca416e","geographic" : { "techno": "GPS", "lng": -3.707135511213621, "lat": 40.40968001418976, "alt": 0, "hdop":3},"tagSensorData" : [{"type" : "batteryLevel","value" : "2.862"}]} 2021-10-07 17:15:53,541 - web2py.app.locationEngine - INFO - received msg from tag : : {"tagId": "ca026847cfca416e", "anchor": [{"RSSI": -70, "ID": "P ID 00021f"}, {"RSSI": -80, "ID": "P ID 00021d"}, {"RSSI": -82, "ID": "P ID 00010d"}], "tagData": {"batteryLevel": 2.856}} 2021-10-07 17:16:53,553 - web2py.app.locationEngine - INFO - received msg from tag : : {"tagId": "ca026847cfca416e", "anchor": [{"RSSI": -63, "ID": "P ID 00021f"}, {"RSSI": -77, "ID": "P ID 00021d"}, {"RSSI": -89, "ID": "P ID 00010d"}], "tagData": {"batteryLevel": 2.856}} 2021-10-07 17:16:53,574 - web2py.app.locationEngine - INFO - Payload : {"tagId" : "ca026847cfca416e","geographic" : { "techno": "GPS", "lng": -3.707135511213621, "lat": 40.40968001418976, "alt": 0, "hdop":3},"tagSensorData" : [{"type" : "batteryLevel","value" : "2.856"}]} 2021-10-07 17:17:53,971 - web2py.app.locationEngine - INFO - received msg from tag : : {"tagId": "ca026847cfca416e", "anchor": [{"RSSI": -75, "ID": "P ID 00021f"}, {"RSSI": -80, "ID": "P ID 00021d"}, {"RSSI": -86, "ID": "P ID 00010d"}, {"RSSI": -90, "ID": "P ID 00010a"}], "tagData": {"batteryLevel": 2.856}} 2021-10-07 17:18:53,720 - web2py.app.locationEngine - INFO - received msg from tag : : {"tagId": "ca026847cfca416e", "anchor": [{"RSSI": -70, "ID": "P ID 00021f"}, {"RSSI": -86, "ID": "P ID 00010d"}, {"RSSI": -86, "ID": "P ID 00021d"}, {"RSSI": -89, "ID": "P ID 00010a"}], "tagData": {"batteryLevel": 2.856}} 2021-10-07 17:18:53,738 - web2py.app.locationEngine - INFO - Payload : {"tagId" : "ca026847cfca416e","geographic" : { "techno": "GPS", "lng": -3.707135511213621, "lat": 40.40968001418976, "alt": 0, "hdop":3},"tagSensorData" : [{"type" : "batteryLevel","value" : "2.856"}]} 2021-10-07 17:19:53,670 - web2py.app.locationEngine - INFO - received msg from tag : : {"tagId": "ca026847cfca416e", "anchor": [{"RSSI": -63, "ID": "P ID 00021f"}, {"RSSI": -82, "ID": "P ID 00021d"}, {"RSSI": -86, "ID": "P ID 00010d"}, {"RSSI": -90, "ID": "P ID 00010a"}], "tagData": {"batteryLevel": 2.856}} 2021-10-07 17:19:53,693 - web2py.app.locationEngine - INFO - Payload : {"tagId" : "ca026847cfca416e","geographic" : { "techno": "GPS", "lng": -3.707135511213621, "lat": 40.40968001418976, "alt": 0, "hdop":3},"tagSensorData" : [{"type" : "batteryLevel","value" : "2.856"}]} 2021-10-07 17:20:53,687 - web2py.app.locationEngine - INFO - received msg from tag : : {"tagId": "ca026847cfca416e", "anchor": [{"RSSI": -66, "ID": "P ID 00021f"}, {"RSSI": -84, "ID": "P ID 00010d"}, {"RSSI": -85, "ID": "P ID 00021d"}, {"RSSI": -87, "ID": "P ID 00010a"}], "tagData": {"batteryLevel": 2.856}}

我的陈述做错了什么?

标签: pythonlineextract

解决方案


推荐阅读