解决TypeError需要解决的问题:需要一个整数(得到类型str)

时间:2018-04-19 20:10:25

标签: python python-3.x python-2.7

我在选择计算年数时遇到问题。

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的了解有限,我无法找到确切的解决方案,而且脚本非常复杂。

这是源代码的一部分,与我的问题有关。

# 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

1 个答案:

答案 0 :(得分:1)

您的错误可能是您忘了将input(...)转换为int

start_year = input('Enter start year')
start_year = int(start_year)

您应该对end_yearoutput执行相同操作。

注意:如果没有源代码,很难帮助您。我需要推断很多东西来帮助你诊断这个错误。