|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Python script for gps log data inserting into mysql
Hi,
I need to write python script to insert the data into mysql from gps log. Suppose Gps log like below, Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. GPSD,R=1 $GPGSV,3,1,11,08,16,328,40,11,36,127,00,28,33,283, 39,20,11,165,00*71 $GPGSV,3,2,11,17,24,208,39,27,11,097,00,13,87,174, 00,19,31,042,00*70 $GPGSV,3,3,11,25,43,033,38,03,07,037,00,23,47,150, 00*44 $GPGGA,111503,0833.6323,N,07652.7685,E,1,04,1.60,1 6.69,M,-94.088,M,,*58 $GPRMC,111503,A,0833.6323,N,07652.7685,E,0.1866,25 6.540,220609,,*2A $GPGSA,A,3,08,28,17,25,,,,,,,,,0.0,1.6,16.3*02 $GPGGA,111504,0833.6316,N,07652.7682,E,1,04,1.60,1 8.70,M,-94.088,M,,*58 - - -- -- When the line starting with $GPRMC and the line status having "A" after two commas, then the line details should be insert into mysql database. line in log like below: $GPRMC,111503,A,0833.6323,N,07652.7685,E,0.1866,25 6.540,220609,,*2A database details: dbname: project tablename: gps fields in table: Quote: insert above line in table like this. time : 111503 latitude : 0833.6323,N longitude : 07652.7685,E speed : 0.1866 date : 220609 Steps: Script has to check whether the line starting with $GPRMC, if it is: then Script has to check for status "A" after two commas ............... if it is ......................... data of that line should insert in mysql db else ................ check next line....up to terminate the log Please help me, how to achieve this task. Thanks, sravan |
|
#2
|
|||
|
|||
|
#!/usr/bin/env python
import MySQLdb file=open("capgps.txt",'r') #Open database connection db = MySQLdb.connect("localhost","root","8868","myproject") # prepare a cursor object using cursor() method cursor = db.cursor() #line is like below #$GPRMC,111503,A,0833.6323,N,07652.7685,E,0.1866,256.540,220609,,*2A for line in file: data=line.split(",") if data[0]=="$GPRMC" and data[2]=="A": if data[4]=="N": latitude=str((data[3])/100.0) else: latitude=str((-data[3])/100.0) if data[5]=="E": longitude=str(data[5]/100.0) else: longitude=str((-data[5])/100.0) #latitude=data[3] #longitude=data[5] # Prepare SQL query to INSERT a record into the database. # sql = "INSERT INTO gps(time, date, latitude, longitude, speed) VALUES(%s,%s,%s,%s,%s)," + "("+data[1]+","+data[9]+","+latitude+","+longitude+","+data[7]+")" linedata = {'time':data[1], 'date':data[9],'latitude':latitude,'longitude':longitude,'speed':data[7]} try: #Execute the SQL command #cursor.execute(sql) cursor.execute("""INSERT INTO gps VALUES (%(id)d, %(time)s, %(date)s, %(latitude)s,%(longitude)s,%(speed)s)""", linedata) # cursor.execute(query,(data[1],data[9],data[3],data[5],data[7])) # Commit your changes in the database db.commit() except: # Rollback in case there is any error db.rollback() # disconnect from server db.close() file.close() i m getting follow error: sravan@gudivada:~/Desktop/pythonscripttest$ ./2read.py Traceback (most recent call last): File "./2read.py", line 15, in <module> latitude=str((data[3])/100.0) TypeError: unsupported operand type(s) for /: 'str' and 'float' please tell me why i am getting this error |
|
#3
|
|||
|
|||
|
Look at the error:
File "./2read.py", line 15, in <module> Something's wrong in line 15. Line 15 is: latitude=str((data[3])/100.0) What's wrong? TypeError: unsupported operand type(s) for /: 'str' and 'float' Something's wrong with the division - you're trying to divide a string of text by a number, and it you can't do that. You need to interpret the text as a number using something like int() or float() first. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Python script for gps log data inserting into mysql |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|