I am dealing with text strings such as the following:
LN1 2DW, DN21 5BJ, DN21 5BL, ...
In Python, how can I count the number of elements between commas? Each element can be made of 6, 7, or 8 characters, and in my example there are 3 elements shown. The separator is always a comma.
I have never done anything related to text mining so this would be a start for me.
SQL Pattern: How can I count the number of comma separated values in a string? Basically, you replace all occurrences of , with an empty string "" , then subtract its LENGTH from the LENGTH of the unadulterated string, which gives you the number of , characters.
If the comma (,
) is the separator, you can simply use str.split
on the string and then len(..)
on the result:
text = 'LN1 2DW, DN21 5BJ, DN21 5B'
number = len(text.split(','))
You can also reuse the list of elements. For instance:
text = 'LN1 2DW, DN21 5BJ, DN21 5B'
tags = text.split(',')
number = len(tags)
#do something with the `tags`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With