简体   繁体   English

使用python将字符串转换为整数

[英]Converting string to integer with python

When trying to convert a string into integer to be used as a variable later in the code, I get the following: 当尝试将字符串转换为整数以在稍后的代码中用作变量时,我得到以下信息:

print int(urlsuccessful[i])

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

locale.atoi()将根据当前的语言环境设置“标记”整数。

If only problems are commas, try: 如果只有问题是逗号,请尝试:

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

int不了解逗号,您需要在尝试转换之前将其删除

You can just do 你可以做

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM