Python's isdigit() method returns False for negative numbers

后端 未结 3 1773
长情又很酷
长情又很酷 2021-01-29 12:35

I have a text files that I read to a list. This list contains integers and strings.

For example, my list could look like this:

[\"name\", \"test\", \"1\"         


        
3条回答
  •  耶瑟儿~
    2021-01-29 13:36

    The way to convert to integers is to try the conversion and be prepared for it to fail:

    for i in range(len(mylist)):
        try:
            mylist[i] = int(mylist[i])
        except ValueError:
            pass
    

提交回复
热议问题