| | |
TypeError: unsupported operand type(s) for /: 'str' and 'float' in python
Thread Solved |
•
•
Join Date: Mar 2009
Posts: 6
Reputation:
Solved Threads: 0
Python Syntax (Toggle Plain Text)
#!/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) 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 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.
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:
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?
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
- Hunter S. Thompson
my photography
•
•
Join Date: Mar 2009
Posts: 6
Reputation:
Solved Threads: 0
hi sir,
Thanks for your reply...
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.
thanks,
sravan
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)
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...
sravan
Last edited by gudivada213; Jun 25th, 2009 at 10:03 am.
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:
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)
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
- Hunter S. Thompson
my photography
•
•
Join Date: Mar 2009
Posts: 6
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- simple addition of variables (Python)
- error in future graph program (Python)
- having difficulties (Python)
- Subtracting values w/list (Python)
- Very Frustrated (Python)
- Easy Noob question re: Print %f" % (Python)
- TypeError-3 (Python)
- Type Erorr nr2 (Python)
Other Threads in the Python Forum
- Previous Thread: Read csv file 10 records at a time
- Next Thread: Python GUI Maker
| Thread Tools | Search this Thread |
alarm app beginner cipher cmd cx-freeze data decimals development dictionary directory dynamic error examples feet file float format function generator getvalue gui halp homework http images import input ip itunes java keycontrol leftmouse line linux list lists logging loop maintain maze millimeter module mouse mysqldb number numbers output parsing path port prime programming projects push py2exe pygame pyglet pymailer pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation split sqlite ssh string strings sudokusolver table terminal text thread threading time tlapse tuple tutorial ubuntu unicode url urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia wx.wizard wxpython xlwt





