簡體   English   中英

如何在python 3中將字符串類型值轉換為int類型值?

[英]How to convert string type value into int type value in python 3?

我需要將字符串轉換為int類型,以便執行操作

 >>> t="'2000'"
 >>> int(t)
 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 ValueError: invalid literal for int() with base 10: "'1'"

你有一個雙引號字符串,它會警告你THR錯誤消息' beeing一個非法數字轉換。 將您的字符串清除為t="10"刪除多余的引號,或者如果從int(t [1:-1])的其他地方收到該字符串,則將其刪除。

t是值為'2000'的字符串。 您正在通過執行t = "2000"來使t成為僅包含數字2000的字符串。 使用int()您還嘗試將撇號也轉換為int,但您不能這樣做。

您正在嘗試將字符串'2000'解析為一個int,因為在2000前后有引號,所以它不是這樣。 您可以更改代碼以使其正常工作:

t = "'2000'"
t = t[1:-1]
int(t)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM