Die ein ausschnitt der Datei die Einzulesen ist sieht folgendermaßen aus:
[codebox=text file=Unbenannt.txt]
I 19:33:10.560 Notification received from 6e400002-b5a3-f393-e0a9-e50e24dcca9e, value: (0x) 31-2C-38-37-34-36-39-32-2C-31-30-32-30-00-00-00-00-00-00-00
A 19:33:10.560 "1,874692,1020 " received
I 19:33:10.561 Notification received from 6e400003-b5a3-f393-e0a9-e50e24dcca9e, value: (0x) 20-20-32-37-31-2C-20-20-31-34-33-2C-20-20-39-37-33-00-00-00
A 19:33:10.561 " 271, 143, 973 " received
I 19:33:10.561 Notification received from 6e400002-b5a3-f393-e0a9-e50e24dcca9e, value: (0x) 31-2C-38-37-34-36-39-34-2C-31-30-32-30-00-00-00-00-00-00-00
A 19:33:10.561 "1,874694,1020 " received
I 19:33:10.597 Notification received from 6e400003-b5a3-f393-e0a9-e50e24dcca9e, value: (0x) 20-20-32-37-31-2C-20-20-31-34-33-2C-20-20-39-37-33-00-00-00
A 19:33:10.597 " 271, 143, 973 " received
I 19:33:10.598 Notification received from 6e400002-b5a3-f393-e0a9-e50e24dcca9e, value: (0x) 31-2C-38-37-34-36-39-35-2C-31-30-32-30-00-00-00-00-00-00-00
A 19:33:10.598 "1,874695,1020 " received
I 19:33:10.599 Notification received from 6e400002-b5a3-f393-e0a9-e50e24dcca9e, value: (0x) 31-2C-38-37-34-37-34-31-2C-31-30-32-30-00-00-00-00-00-00-00
A 19:33:10.599 "1,874741,1020 " received
I 19:33:10.600 Notification received from 6e400003-b5a3-f393-e0a9-e50e24dcca9e, value: (0x) 20-20-32-37-35-2C-20-20-31-34-37-2C-20-20-39-37-32-00-00-00
A 19:33:10.600 " 275, 147, 972 " received
I 19:33:10.601 Notification received from 6e400002-b5a3-f393-e0a9-e50e24dcca9e, value: (0x) 31-2C-38-37-34-37-34-32-2C-31-30-32-30-00-00-00-00-00-00-00
A 19:33:10.601 "1,874742,1020 " received
I 19:33:10.646 Notification received from 6e400003-b5a3-f393-e0a9-e50e24dcca9e, value: (0x) 20-20-32-37-35-2C-20-20-31-34-37-2C-20-20-39-37-32-00-00-00
A 19:33:10.647 " 275, 147, 972 " received
I 19:33:10.649 Notification received from 6e400002-b5a3-f393-e0a9-e50e24dcca9e, value: (0x) 31-2C-38-37-34-37-38-39-2C-31-30-32-30-00-00-00-00-00-00-00
A 19:33:10.649 "1,874789,1020 " received
I 19:33:10.649 Notification received from 6e400003-b5a3-f393-e0a9-e50e24dcca9e, value: (0x) 20-20-32-37-36-2C-20-20-31-34-35-2C-20-20-39-37-32-00-00-00
A 19:33:10.649 " 276, 145, 972 " received
I 19:33:10.650 Notification received from 6e400002-b5a3-f393-e0a9-e50e24dcca9e, value: (0x) 31-2C-38-37-34-37-39-31-2C-31-30-32-30-00-00-00-00-00-00-00
A 19:33:10.650 "1,874791,1020 " received
I 19:33:10.650 Notification received from 6e400003-b5a3-f393-e0a9-e50e24dcca9e, value: (0x) 20-20-32-37-36-2C-20-20-31-34-35-2C-20-20-39-37-32-00-00-00
A 19:33:10.651 " 276, 145, 972 " received
I 19:33:10.695 Notification received from 6e400002-b5a3-f393-e0a9-e50e24dcca9e, value: (0x) 31-2C-38-37-34-37-39-32-2C-31-30-32-30-00-00-00-00-00-00-00
A 19:33:10.695 "1,874792,1020 " received
I 19:33:10.696 Notification received from 6e400002-b5a3-f393-e0a9-e50e24dcca9e, value: (0x) 31-2C-38-37-34-38-33-38-2C-31-30-32-31-00-00-00-00-00-00-00
A 19:33:10.696 "1,874838,1021 " received
I 19:33:10.697 Notification received from 6e400003-b5a3-f393-e0a9-e50e24dcca9e, value: (0x) 20-20-32-37-35-2C-20-20-31-34-38-2C-20-20-39-37-33-00-00-00
A 19:33:10.697 " 275, 148, 973 " received
[/code]
Wie zu sehen ist sind es zwei verschiedene Zeilen, eine beginnt mit I und die andere mit A. Ich möchte folgendes erreichen, es sollen immer die Werte der I-Zeile ausgegeben werden z.B: 1,874741,1020 ---> ID, time, GesamtSensorWert
276, 145, 972 --> Sensor1,Sensor2,Sensor3
Wie aber bei Zeile 28 bis 30 zu sehen ist, entstehen manchmal einige Unregelmäßigkeiten, wo immer zwei gleiche Zeilen aufeinander folgen:
1,874792,1020 ID, times, GesamtSensorWert
1,874838,1021 ID, times, GesamtSensorWert
Mein Ziel ist es dies zu vermeiden d.h. wie im obigen Beispiel, falls so etwas auftritt, wird die vorherige Zeile(rot) übersrpungen und die zweite Zeile (blau) genommen. Ich habe mit Erkennungspunkten gearbeitet um die Zeilen zu erkennen und anhand der Teil der ID, der sich ändert zum erkennen verwendet (6e400002 und 6e400003). Ich bin soweit, dass er mir das erkennt und einen Kommentat ausgibt, ich will es aber so, dass er die Zeilen miteinander vergleicht und falls doppelt, die erste der doppelten nicht wahrnimmt. Könnt Ihr mir helfen?
Dazu auch mein jetziger Code:
Code: Alles auswählen
csv_file_path = 'C:/Users/Luigi/Downloads/python-csv-parser/S1.txt'
csv_file_contents = open(csv_file_path, "r");
BLE_UUID_prefix_1 ="6e400002"
BLE_UUID_prefix_2 ="6e400003"
prev_prefix = 0
repeat = 0
for line in csv_file_contents:
if line.startswith("I"):
if BLE_UUID_prefix_1 in line:
if prev_prefix == BLE_UUID_prefix_1:
repeat = 1
prev_prefix = BLE_UUID_prefix_1
#print prev_prefix
else:
repeat = 0
prev_prefix = BLE_UUID_prefix_1
print prev_prefix
if BLE_UUID_prefix_2 in line:
if prev_prefix == BLE_UUID_prefix_2:
repeat = 1
prev_prefix = BLE_UUID_prefix_2
#print prev_prefix
else:
repeat = 0
prev_prefix = BLE_UUID_prefix_2
print prev_prefix
#continue --> continue wird nichr benötigt (returns control to the beginning of while loop
if line.startswith("A"):
if repeat == 1:
repeat = 0
print "skip line"
if repeat == 0:
#print line
#print [pos for pos, char in enumerate(line) if char == '"']
line_payload = line[16:36]
line_payload = line_payload.strip()
print line_payload
Danke vielmals