Reputation: 1236
I have this script of mine and it is for modifying some data I gather from GPS module. I run this code but it says there is a syntax error and I couldn't understand why there is an error, normally I use that bash command for parsing, can't it be used in a Python loop?
**
import serial
import struct
ser = serial.Serial("/dev/ttyUSB0", 4800, timeout = 1)
file = open("/home/pi/GPSWIFI.csv", "w")
file.write('\n')
for i in range(0,5):
val = ser.readline();
print >> file ,i,',',val
cat /home/pi/GPSWIFI.csv | grep GPGGA | cut -c19-42 >GPSWIFIMODIFIED.csv
file.close()
**
Thanks in advance.
Upvotes: 2
Views: 4209
Reputation: 881
running bash commands in python can be done using os.system
function, or, more recommended, through subprocess.Popen
. You can find more info here:
https://docs.python.org/2/library/subprocess.html#popen-constructor
It would be better if you used python-specific implementation instead, like this:
import serial
ser = serial.Serial("/dev/ttyUSB0", 4800, timeout = 1)
file = open("/home/pi/GPSWIFIMODIFIED.csv", "w")
file.write('\n')
for i in range(0,5):
val = ser.readline();
if val.find("GPGGA")==-1: continue
print >> file ,i,',',val[18:42]
file.close()
note that slice in python (this val[18:42]
) is indexed from 0
Upvotes: 2