ValueError:带基数10:‘的int()文本无效我正在创建一个读取文件的程序,如果文件的第一行不是空白,它将读取接下来的四行。在这些行上执行计算,然后读取下一行。如果该行不是空的,它将继续。但是,我得到了这个错误:ValueError: invalid literal for int() with base 10: ''.它正在读取第一行,但不能将其转换为整数。我能做些什么来解决这个问题?守则:file_to_read = raw_input("Enter file name of tests (empty string to end program):")try:
infile = open(file_to_read, 'r')
while file_to_read != " ":
file_to_write = raw_input("Enter output file name (.csv will be appended to it):")
file_to_write = file_to_write + ".csv"
outfile = open(file_to_write, "w")
readings = (infile.readline())
print readings while readings != 0:
global count
readings = int(readings)
minimum = (infile.readline())
maximum = (infile.readline())
3 回答
慕码人2483693
TA贡献1860条经验 获得超9个赞
>>> int('55063.000000')Traceback (most recent call last): File "<stdin>", line 1, in <module>ValueError: invalid literal for int() with base 10: '55063.000000'
>>> float('55063.000000') 55063.0
慕勒3428872
TA贡献1848条经验 获得超6个赞
for line in open(fname): if line.strip(): # line contains eol character(s) n = int(line) # assuming single integer on each line
h = open(fname)for line in h: if line.strip(): [int(next(h).strip()) for _ in range(4)] # list of integers
h.next()
next(h)
ValueError
int
try: int('')except ValueError: pass # or whatever
明月笑刀无情
TA贡献1828条经验 获得超4个赞
将整数的字符串表示形式传递给 int
将浮点数的字符串表示形式传递到 float
将整数的字符串表示形式传递给 float
通过浮子进入 int
将整数传递给 float
ValueError
int
int
>>> int('5')5>>> float('5.0')5.0>>> float('5')5.0>>> int(5.0)5>>> float(5)5.0>>> int('5.0')Traceback (most recent call last): File "<stdin>", line 1, in <module>ValueError: invalid literal for int() with base 10: '5.0'>>> int(float('5.0')) 5
添加回答
举报
0/150
提交
取消