ValueError:帶基數(shù)10:‘的int()文本無效我正在創(chuàng)建一個讀取文件的程序,如果文件的第一行不是空白,它將讀取接下來的四行。在這些行上執(zhí)行計算,然后讀取下一行。如果該行不是空的,它將繼續(xù)。但是,我得到了這個錯誤:ValueError: invalid literal for int() with base 10: ''.它正在讀取第一行,但不能將其轉(zhuǎn)換為整數(shù)。我能做些什么來解決這個問題?守則: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條經(jīng)驗 獲得超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條經(jīng)驗 獲得超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條經(jīng)驗 獲得超4個贊
將整數(shù)的字符串表示形式傳遞給 int
將浮點數(shù)的字符串表示形式傳遞到 float
將整數(shù)的字符串表示形式傳遞給 float
通過浮子進入 int
將整數(shù)傳遞給 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
提交
取消