invalid literal for int() with base 10
importcsvfilename = 'death_valley_2018_simple.csv'
with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)
tmax=[]
for row in reader:
tmax.append(int(row))
print(tmax)
想要打印出第4列的最高温度值,请问这代码哪里不对?
搜索了这个错误,说是因为含有浮点型,但是我检查了CSV文件,都是整型数字啊 啊,是因为有一行缺失了数据
请问这种情况要怎么办 缺数据的事楼主自己解决,我负责帮你绕开有问题的数据
importcsv
filename = 'death_valley_2018_simple.csv'
with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)
tmax = []
for row in reader:
x = row . strip()
if len(x) > 0 and (isinstance(x , int) or isinstance(x , float)) :
tmax . append(eval(x))
print(tmax)
jackz007 发表于 2019-11-20 10:53
缺数据的事楼主自己解决,我负责帮你绕开有问题的数据
谢谢!
我刚刚试了try except语句,
except ValueError:
print(date,'missing data')
但是运行以后并没有打印这行字?是为什么。。
importcsv
from matplotlib import pyplot as plt
from datetime import datetime
filename = 'death_valley_2018_simple.csv'
with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)
dates,tmax,tmin=[],[],[]
for row in reader:
try:
high=int(row)
low=int(row)
date=datetime.strptime(row,'%Y/%m/%d')
except ValueError:
print(date,'missing data')
else:
dates.append(date)
tmax.append(high)
tmin.append(low)
fig = plt.figure()
plt.plot(dates,tmax,c='red')
plt.plot(dates,tmin,c='blue')
plt.fill_between(dates,tmax,tmin,facecolor='yellow',alpha=0.5)
plt.title('temperature',fontsize=25)
plt.xlabel('date',fontsize=20)
fig.autofmt_xdate()
plt.ylabel('temperature',fontsize=10)
plt.tick_params(axis='both',which='major',fontsize=14)
plt.show()
还出现了一个问题,ValueError: keyword fontsize is not recognized,这个怎么解决
页:
[1]