invalid literal for int() with base 10: '-'
时间: 2023-10-16 16:21:26 浏览: 32
This error occurs when you try to convert a string that contains a dash (-) character to an integer using the int() function. The int() function only accepts strings that contain digits and an optional sign (+ or -).
To resolve this error, you need to remove the dash (-) character from the string before passing it to the int() function. You can use the replace() method to remove the dash (-) character from the string, like this:
```
string = "123-456"
string_without_dash = string.replace("-", "")
integer = int(string_without_dash)
```
In this example, the replace() method replaces the dash (-) character with an empty string, effectively removing it from the original string. The resulting string, "123456", can be converted to an integer without any issues.