【问题标题】:Parsing GPS Coordinates Python coming from TCP/IP connection解析来自 TCP/IP 连接的 GPS 坐标 Python
【发布时间】:2020-09-25 07:20:12
【问题描述】:

所以我目前正在通过 8888 连接到本地 Linux TCP 服务器,并从我的 Arduino shield Quectel BG96 调制解调器发送 GPS 坐标和温度读数。

然后,我将接收到的数据分别作为温度和位置列发送到 mySQL 数据库。

问题是我的位置坐标是混合的(UTC 时间、long 和 lat 都在一个字符串中),我希望能够在将它们发送到数据库之前将它们分开(我会为此添加额外的列)

如果能在研究分离/解析字符串的最有效方法方面提供任何帮助,我将不胜感激。

我的套接字服务器 Python 脚本

        while True:
        data = connection.recv()
        print >>sys.stderr, 'received "%s"' % data
    print(len(data))
        if (len(data) < 15):
    temp = data
    else:
    Loc = data
    try: 

        
        now=datetime.datetime.utcnow()
        print(now.strftime('%Y-%m-%d %H:%M:%S'))
        #Datain = """INSERT INTO irthermo(temperature, location, TIME) VALUES (%s, %s,%s)""",(temp,Loc,now.strftime('%Y-%m-%d %H:%M:%S'))
                    Datain = "INSERT INTO irthermo(temperature, location, TIME) VALUES (%s,%s,%s)"
        values= (temp,Loc,now.strftime('%Y-%m-%d %H:%M:%S'))   
        cursor.execute(Datain,values)
        con.commit()
        print(cursor.rowcount, "Hello")
    except Error as e:
        print("Error while connecting to Mysql", e)
         

        
finally:
if (con.is_connected()):
    con.close()
    print("connection closed")
    cursor.close()
    # Clean up the connection
    connection.close()

【问题讨论】:

  • 我不确定我是否理解这个问题(而且您可能不知道问题出在哪里)。如果您在解释数据时遇到问题,也许您应该询问我们的姊妹网站“arduino”(您会在本网站的右上角图标中看到列表)。用数据的长度来区分数据类型是很难看的。当你了解了数据格式后,编程部分(在本站可以)应该会更容易。
  • 我的问题是获取 Loc 变量并将其拆分为分离经度纬度等,以便我可以将其分别发送到 MySQL 数据库。[因为现在它只是一个巨大的字符串,即使我不需要像 3D 修复这样的信息 - Altitute etc]...我知道GPS坐标的格式,我什至可以在终端中看到它,这里没有问题。也许回答我自己的问题,但我认为只是使用 split 函数拆分 Loc 字符串并将其存储到数组中,然后添加数组以形成 (5522.343,N) 和 (734573.34E) 的格式,例如
  • BTW print &gt;&gt;sys.stderr, 'received "%s"' % data 似乎是从 C++ 复制而来的。 (并且您应该编辑您的问题,以使 ident 更好(这在 python 中很重要)丑陋的部分是您如何获取数据(您只得到一部分,但您也保存了另一部分)。编辑您的问题,并添加你得到什么,你想要什么(我们不知道你数据库的格式):一个字符串,两个数字(S 和 W 的负数?),等等。
  • 常用工具:split(),或只是切片 ([x:y]),或正则表达式(您知道坐标的格式,并且它们以字母 NWSE 结尾)是最常用的工具。否则需要实现自己的解析器。但是您应该检查 ardiuno 部分,以便以简单的方式放置数据(例如,逗号分隔值、区分字段的字母等)

标签: database parsing tcp gps coordinates


【解决方案1】:

Image of MyPHPAdmin showing the Database values coming inr LAT and LONG我设法制作了这样的东西,现在对我有用:

        print(len(data))
        if (len(data) < 15) :
    if ">" in data.strip():
        temp = data
        temp1 = temp.split(">")
        print(temp1[1])
    else:
    Loc = data
    try: 
        loc1 = Loc.split(": ")
        loc2 = loc1[1]
        # setting the maxsplit parameter to 1, will return a list with 2 elements!
        loc3 = loc2.split(",")
        if len(loc3) >= 4 :
            LAT=loc3[1]+loc3[2]
            LONG=loc3[3]+loc3[4]
            now=datetime.datetime.utcnow()
            print(now.strftime('%Y-%m-%d %H:%M:%S'))
            #Datain = """INSERT INTO irthermo(temperature, location, TIME) VALUES (%s, %s,%s)""",(temp,Loc,now.strftime('%Y-%m-%d %H:%M:%S'))
                        Datain = "INSERT INTO irthermo(temperature, Lat, Lon, TIME) VALUES (%s,%s,%s,%s)"
            values= (temp1[1],LAT,LONG,now.strftime('%Y-%m-%d %H:%M:%S'))   
            cursor.execute(Datain,values)
            con.commit()
        else:
                LAT=loc3[1]+loc3[2]
            LONG="Unknown"
            now=datetime.datetime.utcnow()
            print(now.strftime('%Y-%m-%d %H:%M:%S'))
            #Datain = """INSERT INTO irthermo(temperature, location, TIME) VALUES (%s, %s,%s)""",(temp,Loc,now.strftime('%Y-%m-%d %H:%M:%S'))
                        Datain = "INSERT INTO irthermo(temperature, Lat, Lon, TIME) VALUES (%s,%s,%s,%s)"
            values= (temp1[1],LAT,LONG,now.strftime('%Y-%m-%d %H:%M:%S'))   
            cursor.execute(Datain,values)
            con.commit()
    except Error as e:
        print("Error transfer Data", e)

【讨论】:

    猜你喜欢
    • 2013-01-11
    • 2018-05-13
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-28
    • 1970-01-01
    相关资源
    最近更新 更多