Reputation: 3
MouseCoordinates = ";682.9921875,844.421875;682.9921875,844.421875;682.9921875,844.421875;682.9921875,844.421875;682.9921875,844.421875;682.9921875,844.421875;685.07421875,843.3828125;703.69921875,835.79296875;738.93359375,817.82421875;794.69921875,777.19921875;827.13671875,745.4765625;837.0234375,724.93359375;829.33203125,711.79296875;800.76953125,704.26953125;755.41015625,699.5390625;708.44921875,692.27734375;685.23046875,690.44921875;676.109375,691.7265625;676.26171875,707.34375;693.2578125,730.1328125;721.04296875,737.0625;754.38671875,726.58984375;774.35546875,696.18359375;767.9609375,678.54296875;742.44921875,674.99609375;696.72265625,694.90234375;651.9375,741.7890625;635.41015625,787.42578125;641.4765625,812.93359375;655.89453125,828.0078125;659.8359375,831.09375;662.0390625,834.1796875;663.88671875,836.62109375;664.56640625,837.3125;665.47265625,838.234375;665.69921875,838.92578125;665.69921875,840.72265625;665.69921875,843.39453125;665.69921875,844.31640625;665.69921875,844.546875;665.69921875,844.546875;665.69921875,844.546875"
for point in MouseCoordinates.split(";"):
print(point)
isCoordX = True
for coord in point.split(","):
print(coord)
floatCoord = float(coord)
print(floatCoord)
When I run the above code, I get an error that it can't convert string to float. But when I comment out parts of my code, like this:
MouseCoordinates = ";682.9921875,844.421875;682.9921875,844.421875;682.9921875,844.421875;682.9921875,844.421875;682.9921875,844.421875;682.9921875,844.421875;685.07421875,843.3828125;703.69921875,835.79296875;738.93359375,817.82421875;794.69921875,777.19921875;827.13671875,745.4765625;837.0234375,724.93359375;829.33203125,711.79296875;800.76953125,704.26953125;755.41015625,699.5390625;708.44921875,692.27734375;685.23046875,690.44921875;676.109375,691.7265625;676.26171875,707.34375;693.2578125,730.1328125;721.04296875,737.0625;754.38671875,726.58984375;774.35546875,696.18359375;767.9609375,678.54296875;742.44921875,674.99609375;696.72265625,694.90234375;651.9375,741.7890625;635.41015625,787.42578125;641.4765625,812.93359375;655.89453125,828.0078125;659.8359375,831.09375;662.0390625,834.1796875;663.88671875,836.62109375;664.56640625,837.3125;665.47265625,838.234375;665.69921875,838.92578125;665.69921875,840.72265625;665.69921875,843.39453125;665.69921875,844.31640625;665.69921875,844.546875;665.69921875,844.546875;665.69921875,844.546875"
for point in MouseCoordinates.split(";"):
# print(point)
isCoordX = True
for coord in point.split(","):
print(coord)
# floatCoord = float(coord)
# print(floatCoord)
I don't see any characters that would be interfering with the conversion. For example, line one is 682.9921875
. I am a begginner, so it could be something obvious, but I would really appreciate the help. Thanks!
Upvotes: 0
Views: 146
Reputation: 17176
As comments indicated the issue is the first ';' causes an empty string in one of the splits.
An easy fix is:
for point in MouseCoordinates.split(";"):
if point: # Added to block empty strings
print(point)
isCoordX = True
for coord in point.split(","):
if coord:
floatCoord = float(coord)
print(floatCoord)
Upvotes: 0
Reputation: 176
As @martineau said, the first character ';' cause the error. When you split the string with ';' the resulting list have an empty string '' as first element.
Python cannot convert it.
float('')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float:
Upvotes: 1