# ERROR :- string to float in Python

#### mkbutan

Joined Sep 30, 2008
289
I am using the same codes as in the Video on my PC with Windows 10 (64Bits)
Arduino IDE Ver 1.8.13
Python Ver 3.9.0
using IDLE for Python

Iam new to Python using for the First Time with Arduino

Learning the Working of 10DOF from the YouTube Source
and getting the following ERROR

File "C:/Users/mkbut/AppData/Local/Programs/Python/Python39/Pyas1.py", line 11, in <module>
x=float(splitPacket[0])
ValueError: could not convert string to float: "1 ' 2 ' 4 ' \r\n"
Arduino Code

Code:
//Python_9Axis_10DOF
//15.10.20
int x=0;
int y=0;
int z=0;

void setup() {
Serial.begin(115200);
}

void loop() {
x=x+1;
y=y+2;
z=z+4;
Serial.print(x);
Serial.print(" ' ");
Serial.print(y);
Serial.print(" ' ");
Serial.print(z);
Serial.println(" ' ");
delay(100);
}
Python Code

Code:
import serial
import time
arduinoData=serial.Serial('com7',115200)
time.sleep(1)
while (1==1):
while(arduinoData.inWaiting()==0):
pass
dataPacket=str(dataPacket, 'utf-8')
splitPacket=dataPacket.split(',')
x=float(splitPacket[0])
y=float(splitPacket[1])
z=float(splitPacket[3])
print("X=",x,"Y=",y,"Z=",z)

the Code is working till Line #11

Python 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
>>>
==== RESTART: C:/Users/mkbut/AppData/Local/Programs/Python/Python39/Pyas1.py ===
["1 ' 2 ' 4 ' \r\n"]
["2 ' 4 ' 8 ' \r\n"]
["3 ' 6 ' 12 ' \r\n"]
["4 ' 8 ' 16 ' \r\n"]
["5 ' 10 ' 20 ' \r\n"]
["6 ' 12 ' 24 ' \r\n"]
["7 ' 14 ' 28 ' \r\n"]
["8 ' 16 ' 32 ' \r\n"]
["9 ' 18 ' 36 ' \r\n"]
...
...
...
...
...
...

["412 ' 824 ' 1648 ' \r\n"]
["413 ' 826 ' 1652 ' \r\n"]
["414 ' 828 ' 1656 ' \r\n"]
["415 ' 830 ' 1660 ' \r\n"]
["416 ' 832 ' 1664 ' \r\n"]
["417 ' 834 ' 1668 ' \r\n"]
after that the ERROR is coming

#### hrs

Joined Jun 13, 2014
253
Try
Code:
splitPacket = [field.strip() for field in dataPacket.split(',')]

#### upand_at_them

Joined May 15, 2010
518
Looks like the packets have apostrophes not commas.

#### WBahn

Joined Mar 31, 2012
26,148
If someone asked YOU to convert "1 ' 2 ' 4 ' \r\n" to a floating point value, what floating point value would you say it should be?

If you can't do it, how can you expect Python to do it.

You need to clearly describe the behavior that you NEED. Only then does it become possible to figure out how to design and implement a solution that will meet that need.

You SEEM to be wanting to pull certain pieces of that string out and convert them to floating point values. But the pieces you want to pull out have indices 0, 1, and 3. I can't see anyway that that makes sense. So, again, you need to clearly describe what you NEED to have happen.

#### mkbutan

Joined Sep 30, 2008
289
Looks like the packets have apostrophes not commas.
Thanks dear deleted ''
Code:
splitPacket=dataPacket.split(',')
and rewritten and it works and giving the desired OUTPUT

Python OUTPUT
Python 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
>>>
==== RESTART: C:\Users\mkbut\AppData\Local\Programs\Python\Python39\Pyas1.py ===
X= -0.01 Y= -0.01 Z= -0.06
X= -0.01 Y= -0.01 Z= -0.06
X= -0.01 Y= -0.01 Z= -0.06
X= -0.01 Y= -0.01 Z= -0.06
X= -0.01 Y= -0.01 Z= -0.06
X= -0.01 Y= -0.01 Z= -0.06
X= -0.01 Y= -0.01 Z= -0.06
but the serial Output is very fast , how to make it Slow like one line /Sec.

Arduino OUTPUT
08:54:52.866 -> -0.01,-0.01,-0.01,-0.06,-0.06,-0.06,-0.06,-0.06,-0.06
08:54:52.968 -> -0.01,-0.01,-0.01,-0.06,-0.06,-0.06,-0.06,-0.06,-0.06
08:54:53.071 -> -0.01,-0.01,-0.01,-0.06,-0.06,-0.06,-0.06,-0.06,-0.06
08:54:53.172 -> -0.01,-0.01,-0.01,-0.06,-0.06,-0.06,-0.06,-0.06,-0.06
08:54:53.275 -> -0.01,-0.01,-0.01,-0.06,-0.06,-0.06,-0.06,-0.06,-0.06
and how can i get the Python Output like the Arduino Output with the Time Log
like
08:54:52.866 ->X= -0.01 Y= -0.01 Z= -0.06

#### mkbutan

Joined Sep 30, 2008
289
If someone asked YOU to convert "1 ' 2 ' 4 ' \r\n" to a floating point value, what floating point value would you say it should be?

If you can't do it, how can you expect Python to do it.

You need to clearly describe the behavior that you NEED. Only then does it become possible to figure out how to design and implement a solution that will meet that need.

You SEEM to be wanting to pull certain pieces of that string out and convert them to floating point values. But the pieces you want to pull out have indices 0, 1, and 3. I can't see anyway that that makes sense. So, again, you need to clearly describe what you NEED to have happen.

Thanks dear sir for your Time to ans the Query...
as I said I am new to the Python
althow I am using the Arduino from last 7-9 years just (in Home not in any Professional use ) and few days back I was watching the YouTube Video and one new thing came to know that Arduino Serial OUTPUT can be Used in Python Serial and cam be view in 3D in Python that made me Curious to Download the Python and Restarted to work on the Arduino and Python Code he provided ...

and by the time you posted I got the desired Output of Python in my post #5