我在python 2.7上收到以下错误 . 'Hello'是文本文件中一行中的最后一个单词 . 以下解决方案对我不起作用ValueError: invalid literal for int() with base 10: 'Height (mm)'
ValueError: invalid literal for int() with base 10: 'hello.'
执行以下代码时
import io
with io.open(r'C:\Python\Data\somefile.txt','r+') as f:
bytecolumn = (line.rsplit(None,1)[1] for line in f)
bytes = (int(x) for x in bytecolumn if x != '-')
print('Total', sum(bytes))
我也试过用
int(float(x))
但它会引发错误
ValueError: could not convert string to float: hello.
P.S - 我已经在堆栈溢出中查看了所有类似的问题,没有一个解决方案有效 . 这是发布的原因 . 请不要标记重复
Edit :我想找到这个词的大小 . 这就是我做总和并调用生成器的原因
1 回答
以下解决方案有效 . 我们需要使用而不是使用
int(x)
所以最终的代码更新为