解决TypeError的解决方法:需要一个整数(得到类型str) [英] what to fix to solve the TypeError: an integer is required (got type str)
问题描述
我在选择计算年份时遇到问题.
I have a problem with the choice of calculation years.
python flux2nc.py ../data/output/fluxes/ ../data/output/
IMPORTANT: ../data/output/fluxes/ SHOULD CONTAIN ONLY FLUXES FILES!!!
Choose output parameter
1 - Precipitation
2 - Evapotranspiration
3 - Runoff
4 - Base flow
5 - Snow Water Equivalent
6 - Soil moisture
Choose output (1 a 6)>3
Enter start year:2011
End year:2012
Traceback (most recent call last):
File "flux2nc.py", line 240, in <module>
main()
File "flux2nc.py", line 234, in main
flux2nc(sys.argv[1],sys.argv[2])
File "flux2nc.py", line 120, in flux2nc
inidate = dt.date(start_year,1,1)
TypeError: an integer is required (got type str)
我知道这个问题已经提出了,但是由于我对python的了解有限,我找不到确切的解决方案,而且脚本非常复杂.
I know that this problem is already posed, but I can't find the exact solution given my limited knowledge on python, and the script is pretty much complicated.
这是源代码的一部分,与我的问题有关.
here is the part of the source code, related to my question.
# import dependencies
import sys
import os, string
# handle dates...
import datetime as dt
# NetCDF and Numeric
from netCDF4 import *
from numpy import *
# if the date information is not set get it from user
if start_year == None:
# for what date?
start_year = input("Enter start year:")
if end_year == None:
end_year = input("End year:")
# set date information in datetime object
inidate = dt.date(start_year,1,1)
enddate = dt.date(end_year,12,31)
# calculate number of days in time series
days = enddate.toordinal() - inidate.toordinal()+1
推荐答案
您的错误可能是您忘记将 input(...)
强制转换为 int
:
Your error is probably that you forgot to cast the input(...)
to int
:
start_year = input('Enter start year')
start_year = int(start_year)
你应该对 end_year
和 output
做同样的事情.
you should do the same for end_year
and output
.
注意:如果没有源代码,很难尝试帮助您.我需要推断很多事情来帮助您诊断此错误.
Note: It's really hard to try to help you without the source code. I need to infer a lot of things to help you diagnosis this error.
这篇关于解决TypeError的解决方法:需要一个整数(得到类型str)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!