From 14ac367a5d17ca641faf9f07a46d030f5dc516dd Mon Sep 17 00:00:00 2001 From: Fisch Date: Fri, 8 Sep 2023 08:09:57 +0200 Subject: [PATCH] add csv to lineprotocol converter for influxdb --- .../csvToLineprotocol/csvToLineprotocol.py | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 logdata_visualization/csvToLineprotocol/csvToLineprotocol.py diff --git a/logdata_visualization/csvToLineprotocol/csvToLineprotocol.py b/logdata_visualization/csvToLineprotocol/csvToLineprotocol.py new file mode 100644 index 0000000..e844bec --- /dev/null +++ b/logdata_visualization/csvToLineprotocol/csvToLineprotocol.py @@ -0,0 +1,57 @@ +import math +import argparse + +import csv + +parser = argparse.ArgumentParser( + prog = 'File Edit Script', + description = 'Read content of text file and write to file', + epilog = '') + +parser.add_argument('filename') # positional argument +parser.add_argument('-o', '--output') # option that takes a value +parser.add_argument('-v', '--verbose', action='store_true') # on/off flag + +args = parser.parse_args() +print(args.filename, args.output, args.verbose) + +input_filename=args.filename +output_filename=args.output +if (output_filename is None): + output_filename=input_filename+".lp" + +measurement="bobbycar" + +with open(input_filename, newline='') as csvfile: + csvdict = csv.DictReader(csvfile) + + with open(output_filename, 'w', newline='') as wf: + for irow,row in enumerate(csvdict): + if (irow%12!=0): + continue + #print(row['time'], row['cmd_FrontL']) + #csvwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam']) + output_timestamp=int(float(row['timestamp'])*1000000000) #csv timestamp is in seconds, convert to ns + #output_timestamp=int(float(row['timestamp'])*1000) #timestamp in ms +# print(list(row.keys())[2:8]) + for key in list(row.keys()): + if (key != 'timestamp' and key != 'time'): + wf.write(measurement+' '+key+'='+row[key]+' '+str(output_timestamp)) + wf.write('\n') + + +''' + +lines=None +with open(args.filename,'r') as rf: + lines=rf.readlines() + +lines=[x.rstrip() for x in lines] #remove newline and whitespaces at end + +#modify content of lines here + +with open(args.output, 'w') as wf: + for line in lines: + wf.write(line) + wf.write('\n') +'''