Reputation: 60
I will have a string and I want to check that it is in the format "float, float, float, float". There will be 3 commas and four floats, I don't know how many decimal places long the floats are gonna be. so I want to check it without using re class. I need to extract all four floats after checking the string. so 3 floats before a comma and 1 last after comma.
I can't find a string function to do that. I have checked my python reference book and still can't find a method. I usually code in C++ and recently begun Python. Thank you for your help.
Upvotes: 0
Views: 139
Reputation: 1842
Here's my attempt at your problem.
# Returns the list of four floating numbers if match and None otherwise
def four_float(str_input):
# Split into individual floats
lst = str_input.split(',')
for flt in lst:
# Check for the existence of a single dot
d_parts = flt.strip().split('.')
if len(d_parts) != 2:
return None
else:
# Check that the chars on both sides of the dot are all digits
for d in d_parts:
if not d.isdigit():
return None
return [float(n) for n in lst]
Upvotes: 1