英文:
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('%Y')
for y in range(start_year,today_year):
print('Outer loop enter for year', y)
Some function here which I want to do.............
for x in range(start_month,13):
print('Inner loop enter for month number',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)
<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' 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('%Y')
.
Just replace the line
today_year=today.strftime('%Y')
with
today_year=int(today.strftime('%Y'))
As pointed out by Stargazer, you could do,
today_year = today.year
instead of converting the str
to int
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论