invalid literal for int() with base 10: '45 days'
时间: 2023-10-16 22:23:09 浏览: 29
This error occurs because you are trying to convert the string '45 days' into an integer using the int() function, but '45 days' is not a valid integer.
If you want to extract the number '45' from the string '45 days', you can use string manipulation techniques to extract the numeric part of the string and convert it to an integer.
For example, you could use the split() method to split the string into a list of words, and then use indexing to extract the first word (which should be the numeric part of the string). Then you can use int() to convert that string to an integer:
```
my_string = '45 days'
my_number = int(my_string.split()[0])
```
This will set `my_number` to the integer value 45.
Alternatively, if you only want to extract the numeric part of the string and don't need to convert it to an integer, you can use regular expressions to match the numeric part of the string:
```
import re
my_string = '45 days'
match = re.search(r'\d+', my_string)
if match:
my_number = match.group()
```
This will set `my_number` to the string value '45'.