TypeError: ‘str’对象不能被解释为整数的Python for循环

huangapple go评论165阅读模式
英文:

TypeError: 'str' object cannot be interpreted as an integer python for loop

问题

我正在尝试创建一个函数,该函数从给定的年份和月份开始返回数据,直到今天的年份和月份。为了简单起见,我已经用外部循环和内部循环的打印语句代替了我希望函数执行的操作。我收到了错误消息TypeError: 'str'对象无法解释为整数

定义函数

def frequency_database(df, start_month, start_year):
    data = pd.DataFrame([])
    import datetime
    start_month = int(start_month)
    start_year = int(start_year)
    today = datetime.date.today()
    today_year = today.strftime('%Y')

    for y in range(start_year, today_year):
        print('Outer loop enter for year', y)
        # 这里是我想要执行的某些操作.............
        for x in range(start_month, 13):
            print('Inner loop enter for month number', x)
            # 这里是我想要执行的某些操作.............

调用函数

frequency_database(df, 1, 2015)

错误

TypeError: 'str'对象无法解释为整数

堆栈跟踪如下所示

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-37-06567ae5d027> in <module>
     12         print('Inner loop enter for month number', x)
     13 
---> 14 frequency_database(df,1,2015)

<ipython-input-37-06567ae5d027> in frequency_database(df, start_month, start_year)
      7     today_year=today.strftime('%Y')
      8 
----> 9     for y in range(start_year,today_year):
     10         print('Outer loop enter for year', y)
     11         for x in range(start_month,13):

TypeError: 'str'对象无法解释为整数
英文:

I am trying to create a function that returns data starting from a given year and given month to today's year and month. For simplicity I have replaced what I want my function to do with print statements of outer loop and inner loop. I am getting error TypeError: 'str' object cannot be interpreted as an integer

Defining function

def frequency_database(df,start_month,start_year):
    data = pd.DataFrame([])
    import datetime
    start_month=int(start_month)
    start_year=int(start_year)
    today = datetime.date.today()
    today_year=today.strftime(&#39;%Y&#39;)

    for y in range(start_year,today_year):
        print(&#39;Outer loop enter for year&#39;, y)
        Some function here which I want to do.............
        for x in range(start_month,13):
            print(&#39;Inner loop enter for month number&#39;,x)
            Some function here which I want to do.............

Calling funtion

frequency_database(df,1,2015)

Error

TypeError: 'str' object cannot be interpreted as an integer

Stack Trace as requested

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-37-06567ae5d027&gt; in &lt;module&gt;
     12             print(&#39;Inner loop enter for month number&#39;,x)
     13 
---&gt; 14 frequency_database(df,1,2015)

&lt;ipython-input-37-06567ae5d027&gt; in frequency_database(df, start_month, start_year)
      7     today_year=today.strftime(&#39;%Y&#39;)
      8 
----&gt; 9     for y in range(start_year,today_year):
     10         print(&#39;Outer loop enter for year&#39;, y)
     11         for x in range(start_month,13):

TypeError: &#39;str&#39; object cannot be interpreted as an integer

答案1

得分: 5

问题是你尝试将range赋予一个str,即today_year=today.strftime('%Y')

只需将以下行

today_year=today.strftime('%Y')

替换为

today_year=int(today.strftime('%Y'))

正如Stargazer所指出的,你也可以这样做:

today_year = today.year

而不必将str转换为int

英文:

The problem is you tried to give range a str which is today_year=today.strftime(&#39;%Y&#39;).

Just replace the line

today_year=today.strftime(&#39;%Y&#39;)

with

today_year=int(today.strftime(&#39;%Y&#39;))

As pointed out by Stargazer, you could do,

today_year = today.year

instead of converting the str to int

huangapple
  • 本文由 发表于 2020年1月3日 17:11:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定