TypeError: unsupported operand type(s) for /: 'str' and 'float' in python

Thread Solved

Join Date: Mar 2009
Posts: 6
Reputation: gudivada213 is an unknown quantity at this point 
Solved Threads: 0
gudivada213 gudivada213 is offline Offline
Newbie Poster

TypeError: unsupported operand type(s) for /: 'str' and 'float' in python

 
0
  #1
Jun 25th, 2009
  1. #!/usr/bin/env python
  2. import MySQLdb
  3.  
  4. file=open("capgps.txt",'r')
  5. #Open database connection
  6. db = MySQLdb.connect("localhost","root","8868","myproject")
  7. # prepare a cursor object using cursor() method
  8. cursor = db.cursor()
  9. #line is like below
  10. #$GPRMC,111503,A,0833.6323,N,07652.7685,E,0.1866,256.540,220609,,*2A
  11. for line in file:
  12. data=line.split(",")
  13. if data[0]=="$GPRMC" and data[2]=="A":
  14. if data[4]=="N":
  15. latitude=str((data[3])/100.0)
  16. else:
  17. latitude=str((-data[3])/100.0)
  18. if data[5]=="E":
  19. longitude=str(data[5]/100.0)
  20. else:
  21. longitude=str((-data[5])/100.0)
  22. linedata = {'time':data[1], 'date':data[9],'latitude':latitude,'longitude':longitude,'speed':data[7]}
  23.  
  24. try:
  25. #Execute the SQL command
  26. #cursor.execute(sql)
  27. cursor.execute("""INSERT INTO gps VALUES (%(id)d, %(time)s, %(date)s, %(latitude)s,%(longitude)s,%(speed)s)""", linedata)
  28. # cursor.execute(query,(data[1],data[9],data[3],data[5],data[7]))
  29. # Commit your changes in the database
  30. db.commit()
  31. except:
  32. # Rollback in case there is any error
  33. db.rollback()
  34. # disconnect from server
  35. db.close()
  36. file.close()

i got below 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 explain why i am getting this error...

thanks,
sravan
Last edited by gudivada213; Jun 25th, 2009 at 8:29 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: TypeError: unsupported operand type(s) for /: 'str' and 'float' in python

 
0
  #2
Jun 25th, 2009
Because when you read lines from a file, a string is returned. Your data[3] is "0833.6323" (as a string). You can just convert it to a float first before the calculation like this:
latitude=str(float(data[3])/100.0)

EDIT:
I noticed you signed your post "sraven". You already have an account here (under that name) so why did you start this new one?
Last edited by shadwickman; Jun 25th, 2009 at 8:40 am. Reason: noticed dual account
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson

my photography
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 6
Reputation: gudivada213 is an unknown quantity at this point 
Solved Threads: 0
gudivada213 gudivada213 is offline Offline
Newbie Poster

Re: TypeError: unsupported operand type(s) for /: 'str' and 'float' in python

 
0
  #3
Jun 25th, 2009
hi sir,
Thanks for your reply...

linedata = {'time':data[1], 'date':data[9],'latitude':latitude,'longitude':longitude,'speed':data[7]}

try:
#Execute the SQL command
cursor.execute("""INSERT INTO gps VALUES (%(id)d, %(time)s, %(date)s, %(latitude)s,%(longitude)s,%(speed)s)""", linedata)
Now tha script not inserting the data to mysql,
its giving below error:
Traceback (most recent call last):
File "./2read.py", line 29, in <module>
cursor.execute("""INSERT INTO gps VALUES (%(id)d,%(time)s, %(date)s, %(latitude)s,%(longitude)s,%(speed)s)""", linedata)
File "/var/lib/python-support/python2.5/MySQLdb/cursors.py", line 151, in execute
query = query % db.literal(args)
KeyError: 'id'
-------
Id is a column with autoincrement , Not null and it is primary key...
I came to know error is at id, incursor.execute...if i want to insert column woih auto increment...what i have to do.

I l registered with my another email id...but i didnt get any activation key...si i couldnt activate it...today i changed my email id and activated...
thanks,
sravan
Last edited by gudivada213; Jun 25th, 2009 at 10:03 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: TypeError: unsupported operand type(s) for /: 'str' and 'float' in python

 
1
  #4
Jun 25th, 2009
In case you didn't notice, your linedata dictionary has no key "id". Hence you receiving the "KeyError" problem. You need the ID of the column you're inserting this into if I'm correct (I barely touched on MySQL about a year ago). Is the table you're inserting this into blank, and you're building it up? Or does it contain data and you're just overwriting/editing rows?
If you're building up a blank table, why not just track the number of rows you've added to the table so far and then you can use that as the id? Set "rowsadded" to zero at the beginning of your script and then increment it after each row input to the table. That way you can have your linedata dictionary as this:
  1. linedata = {'id':rowsadded, 'time':data[1], 'date':data[9],'latitude':latitude,'longitude':longitude,'speed':data[7]}
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson

my photography
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 6
Reputation: gudivada213 is an unknown quantity at this point 
Solved Threads: 0
gudivada213 gudivada213 is offline Offline
Newbie Poster

Re: TypeError: unsupported operand type(s) for /: 'str' and 'float' in python

 
0
  #5
Jun 25th, 2009
Hi Sir,
My table structure is below,
Now it is empty,
mysql> describe gps;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| time | varchar(30) | NO | | NULL | |
| date | varchar(30) | NO | | NULL | |
| latitude | varchar(30) | NO | | NULL | |
| longitude | varchar(30) | NO | | NULL | |
| speed | varchar(30) | NO | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
6 rows in set (0.42 sec)
I dont need to give id from this python..script.
As your idea,
i have to give rowsadded=0 and then increment for every row...but here the task is not that...i just want to insert that id automatically for every row.

thanks,
sravan
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC