-2

My strings are something like that:

str1 = "3,5 of 5 stars"
str2 = "4 of 5 stars"

I want to extract the first number of each string.

Something like that:

str1 = 3,5
str2 = 4

The Problem is that the numbers are in two formats (int and float)

I hope you guys can help me

Thanks for your help

5 Answers5

0
string = "3 o 4 k 5"

for char in string:
    entry = ""
    try:
        entry = int(char)
    except:
        continue
    if entry != "":
        print entry
        break

Here's the explanation. The string holds the string. As the for loop begins, char is set to the first character in the string. The for loop attempts to convert the character into an integer. If it is successful, it means that it is the character is a number. In that case, it is the first number to be found, so it is outputted and the loop stops.

If the conversion fails, it will output an error (thus the except part) but since we are using try/except, the loop will skip to its next character immediately. The for loop will continue until a number has been found or there are no numbers in the string.

Anthony Pham
  • 3,096
  • 5
  • 29
  • 38
0

I guess your string format is - X of Y stars

You can extract X in this way.

>>> my_str = "3,5 of 5 stars"
>>> my_str.strip().split(' ')[0]
'3,5'

Let's say you want to convert 3,5 to float to do some math on it then you first replace , with . and then wrap around float(...).

>>> float(my_str.strip().split(' ')[0].replace(',','.'))
3.5
Gurupad Mamadapur
  • 989
  • 1
  • 13
  • 24
0

If there is a space before the "of", you can use (avoids regex):

>>> print [item.split()[0] for item in [str1, str2]]
['3,5', '4']
ospahiu
  • 3,465
  • 2
  • 13
  • 24
0

If the pattern of strings is always "X of Y stars" you can do the following:

str1 = "3,5 of 5 stars"
str2 = "4 of 5 stars"

lst = [str1, str2, ...]
nums = [float(x.split(' of ')[0].replace(',','.')) for x in lst]
print(nums)  # prints [3.5, 4.0]
Ma0
  • 15,057
  • 4
  • 35
  • 65
0

To match numbers and floats (using the , delimiter) in a string you could use the re module:

>>> re.findall(r"[-+]?\d*\,\d+|\d+", "5,5 of 5 stars")
['5,5', '5']

>>> re.findall(r"[-+]?\d*\,\d+|\d+", "5,5 of 5 stars")[0]
'5,5'

>>> re.findall(r"[-+]?\d*\,\d+|\d+", "4 of 5 stars")[0]
'4'

I've used the regex from this StackOverflow answer (from @miku) but modified it to use , as delimiter instead of ..

Community
  • 1
  • 1
MSeifert
  • 145,886
  • 38
  • 333
  • 352