I have a simple python script, which reads the serial input and saves it to a file (file.txt).
The serial input is like this:{lat: 41.07494, lng:14.2742},
but sometimes there are errors or wrong values, e.g. {lat: 41.▒▒7494, lng:14.2742},
or something else, so the output has to be filtered and than stored to the file.
I tried to use grep with os, but this didnt work, and I dont think it is the proper way to do it in python.
#!/usr/bin/env python
import time
import serial
import os
a = 1
ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
counter=0
while 1:
val=ser.readline()
print val
f = open('/file.txt', 'a')
f.write(val)
f.close()
os.system("grep '^{lat: [0-9][8-9]\.[0-9]\{5\}, lng:[0-9][0-9]\.[0-9]\{4\}},$' /file.txt > /filtered.txt")
So the only output should be like this:
{lat: xx.xxxxx, lng:xx.xxxxx},