Selaa lähdekoodia

Serial output parser for EM919x.

master
langspielplatte 2 vuotta sitten
vanhempi
commit
a845747a9c
1 muutettua tiedostoa jossa 71 lisäystä ja 0 poistoa
  1. +71
    -0
      format_serial_txt_to_csv_EM9190.py

+ 71
- 0
format_serial_txt_to_csv_EM9190.py Näytä tiedosto

@@ -0,0 +1,71 @@
#!/usr/bin/env python3
import csv
import datetime
import re
from argparse import ArgumentParser
import pandas as pd

KEY_VALUE_REGEX = r"(.+):(.+)"


if __name__ == "__main__":

parser = ArgumentParser()
parser.add_argument("-f", "--file", required=True, help="Input txt file.")
args = parser.parse_args()

file = open(args.file, "r")
content = file.read()
file.close()

header = ["time"]
csv_lines = list()
p = re.compile(KEY_VALUE_REGEX)

for part in content.split(";;;"):
if part == "":
break
part = part.replace("\t", "\n").strip()

csv_line = list()
time = None
for line in part.split("\n"):
if not line.startswith("!") or line == "" or line == "\n":
if time is None:
time = line
csv_line.append(time)
m = p.match(line)
if m:
key = m.group(1).strip().replace(" ", "_")
value = m.group(2).replace("MHz", "").strip()

if key not in header:
header.append(key)

csv_line.append(value)
if len(csv_line) > 1:
#print(csv_line)
csv_lines.append(csv_line)



outputfile = open(args.file.replace("txt", "csv"), "w")
writer = csv.writer(outputfile, delimiter=",", lineterminator="\n", escapechar='\\')
writer.writerow(header)
#print(all_csv_lines)
for l in csv_lines:
#print(l)
writer.writerow(l)

outputfile.close()

outputfile = open(args.file.replace("txt", "csv"), "r")
serial_df = pd.read_csv(outputfile)
serial_df["datetime"] = pd.to_datetime(
serial_df["time"].apply(lambda x: datetime.datetime.fromtimestamp(x))
)
serial_df.to_csv(args.file.replace("txt", "csv"))
outputfile.close()




Loading…
Peruuta
Tallenna