I'm trying to check the validity of comma-separated strings in python. That is, it's possible that the strings contain mistakes whereby there are more than one comma used.
Here is a valid string:
foo = "a, b, c, d, e"
This is a valid string as it is comma-delimited; only one comma, not several or spaces only.
Here is an invalid string:
invalid = "a,, b, c,,,,d, e,,; f, g"
The invalid string is invalid because (1) it uses more than one comma and (2) it also uses a semicolon ;
.
What would be the most effective way to check that the strings are valid?
My first attempt was to try something like:
def check_valid_string(input_string):
if ",," in input_string or ";" in input_string:
return "Not valid" ## or False
else:
return "Valid" ## or True
however, it's not clear that this would catch all possible invalid strings. It's also not clear to me that this approach is the most computationally efficient (i.e. quick).