簡體   English   中英

使用python將字符串轉換為整數

[英]Converting string to integer with python

當嘗試將字符串轉換為整數以在稍后的代碼中用作變量時,我得到以下信息:

print int(urlsuccessful[i])

ValueError: invalid literal for int() with base 10: '2,919,247'

locale.atoi()將根據當前的語言環境設置“標記”整數。

如果只有問題是逗號,請嘗試:

>>> int("2,919,247".replace(",", ""))
2919247

int不了解逗號,您需要在嘗試轉換之前將其刪除

你可以做

def int2str(my_integer):
  return "%d" % my_integer

暫無
暫無

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

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