import csv
from matplotlib import pyplot as plt
filename='death_valley_2014.csv'
with open(filename) as f:hights.append(hight)
#运行,引发异常
Traceback (most recent call last):
File "valueError.py", line 12, in
hight=int(row[1])
ValueError: invalid literal for int() with base 10: ''
后来发现,是因为文件中存在空子符(' ')
要解决这个问题,可以使用try-except-else代码块来处理、remove()、del也可以
我是用的try-except-else
try:
hight=int(row[1])
except ValueError:
pass
else:
hights.append(hight)