[英]Process line by line from output of command line
我有来自源代码的 Python 代码,可以处理 GPRMC 格式。 我修改了数据来自"gpsd -d | awk -F ':' 'BEGIN{OFS=\":\"}{print $2}' | grep GPRMC"
,并且是这样的列表:
$GPRMC,220516,A,5133.82,N,00042.24,W,173.8,231.8,130694,004.2,W*70
$GPRMC,220516,A,5133.82,N,00042.24,W,173.8,231.8,130694,004.2,W*70
所以当我启动它时,我得到了这个错误:
data = ser.readline()
AttributeError: 'str' object has no attribute 'readline
以下是部分代码:
port = "gpsd -d | awk -F ':' 'BEGIN{OFS=\":\"}{print $2}' | grep GPRMC"
print("Receiving GPS data")
ser = port
find = False
while find == False:
print("Waiting for GPRMC data")
data = ser.readline()
if data[0:6] == "$GPRMC":
parsingData = data.split(",")
print(parsingData)
if parsingData[2] == "A":
parseGPS(data)
find = True
请帮我解决这个问题。 ps:我不是 python 编码器,我才几天才开始使用这种语言,抱歉我的英语不好
从我读到的:你跑
gpsd -d | awk -F ':' 'BEGIN{OFS=\":\"}{print $2}' | grep GPRMC
在操作系统中,它返回像
$GPRMC,220516,A,5133.82,N,00042.24,W,173.8,231.8,130694,004.2,W*70
这是我的解决方案。 希望它有所帮助:
from subprocess import Popen, PIPE
from re import search
process = Popen("gpsd -d | awk -F ':' 'BEGIN{OFS=\":\"}{print $2}' | grep GPRMC", stdout=PIPE, shell=True, universal_newlines=True)
while True:
line = str(process.stdout.readline())
if not line:
break
print("Receiving GPS data")
find = False
print(line)
while find == False:
if search(r'$GPRMC', line):
print("Waiting for GPRMC data")
parsingData = line.strip().split(",")
print(parsingData)
if parsingData[2] == "A":
parseGPS(data)
find = True
这里有两个问题。
(1) ser
是一个字符串。 您需要先运行该字符串中的命令,然后才能获得任何 output。
(2) 一旦有 output,你就不会使用readline
这就是你从打开的文件中读取行的方式,你的 output 不会在文件中。
熟悉如何通过 python 运行 OS 命令。 有很多方法可以做到这一点。 如果您不知道从哪里开始,这里有一个很好的概述: https://stackoverflow.com/a/92395
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.