【发布时间】:2017-03-07 00:29:04
【问题描述】:
我正在努力在 Python 中将字符串类型转换为整数。我要做的是从 CSV 文件中读取特定列并将其作为整数传递。这是我的代码:
import sys
for line in sys.stdin:
line = line.strip()
line = line.split(",")
tMax = line [9]
tMax2 = int(tMax)
if len(tMax) <=4:
print (tMax2)
当我运行它时,我收到以下错误,
invalid literal for int() with base 10
我检查了列的类型,它被接受为字符串。
【问题讨论】:
-
字符串中可能有其他一些非数字字符。它仍然是一个字符串,但可能存在空格、点或其他无法使用
int() -
@Signal - 没有解释为什么
It's typically better ..这听起来像你的意见。
标签: python python-3.x mapreduce