解析来自TCP / IP连接的GPS坐标Python

问题描述

因此,我目前正在通过8888连接到本地Linux TCP Server,并从我的Arduino屏蔽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,%s)"
        values= (temp,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()

解决方法

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,%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,values)
            con.commit()
    except Error as e:
        print("Error transfer Data",e)