943,724 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 6944
  • Python RSS
Jun 25th, 2009
0

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

Expand Post »
Python Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gudivada213 is offline Offline
6 posts
since Mar 2009
Jun 25th, 2009
0

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

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
Reputation Points: 186
Solved Threads: 77
Posting Pro in Training
shadwickman is offline Offline
495 posts
since Jul 2007
Jun 25th, 2009
0

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

hi sir,
Thanks for your reply...

Quote ...
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.

Quote ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gudivada213 is offline Offline
6 posts
since Mar 2009
Jun 25th, 2009
1

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

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:
Python Syntax (Toggle Plain Text)
  1. linedata = {'id':rowsadded, 'time':data[1], 'date':data[9],'latitude':latitude,'longitude':longitude,'speed':data[7]}
Reputation Points: 186
Solved Threads: 77
Posting Pro in Training
shadwickman is offline Offline
495 posts
since Jul 2007
Jun 25th, 2009
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gudivada213 is offline Offline
6 posts
since Mar 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Read csv file 10 records at a time
Next Thread in Python Forum Timeline: Python GUI Maker





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC