开发者社区> 问答> 正文

Python类-+不支持的操作数类型:“ int”和“ str”

在Google表格中,我有一个包含用户回复的列表。根据Google API,我从Google表格中获取数据并将其放入python列表中。

然后,我定义了一个类,在这个类中,我正在计算值的分数。调用coreCalc()时,出现以下错误消息:

Traceback (most recent call last):
  File "coreCalc.py", line 52, in <module>
    coreCalc()
  File "coreCalc.py", line 49, in __init__
    print(sum(df_array))
TypeError: unsupported operand type(s) for +: 'int' and 'str'

我该如何解决这个问题,我的课定义不正确,因此无法避免此错误?

我的代码[请注意格式,我是从VIM复制的]:

  7 #references
  8 import gspread
  9 from oauth2client.service_account import ServiceAccountCredentials
 10 import pandas as pd
 11 import csv
 12 import time
 13 import requests
 14 
 15 #use creds to create a client to interact with the Google Drive API
 16 scope = ['https://spreadsheets.google.com/feeds',
 17          'https://www.googleapis.com/auth/drive']
 18 creds = ServiceAccountCredentials.from_json_keyfile_name('/Users/Miauw/Miaauw/github/ComplexService/Token/client_secret.json', scope)
 19 client = gspread.authorize(creds)
 20 
 21 # Find a workbook by name and open the first sheet
 22 sheet = client.open("IFTTT_Webhooks_Events").sheet1
 23 
 24 #get data directly from google sheet.
 25 data = sheet.get_all_values()
 26 headers = data.pop(0)
 27 # capture data into dataframe
 28 df = pd.DataFrame(data, columns=headers)
 29 # obtain values from the last row of the spreadsheet. 
 30 df_array = [ (df["Statement 1"].iloc[-1]),
 31     (df["Statement 2"].iloc[-1]),
 32     (df["Statement 3"].iloc[-1]),
 33     (df["Statement 3"].iloc[-1])
 34 ]
 35 
 36 #function for calculating user lonelienss based on UCLA Lonliness scoring (theory) 
 37 class coreCalc:
 38     def __init__(self):
 39     #assign loneliness-scores to the various user-responses
 40         for i in range(len(df_array)):
 41             if df_array[i] == 'Often':
 42                 df_array[i] = 3
 43             elif df_array[i] == 'Sometimes':
 44                 df_array[i] = 2
 45             elif df_array[i] == 'Rarely':
 46                 df_array[i] = 1
 47             elif df_array[i] == 'Never':
 48                 df_array[i] = 0
 49             #obtain sum of values for final scoring
 50                 print(sum(df_array))
 50 
 51
 52  coreCalc()

附加信息:

如果我打印(df_array),结果是:

['Often', 'Never', 'Never', 'Never']

分配分数后,我得到:

[3, 0, 0, 0]

我想要的就是sum()。

问题来源:stackoverflow

展开
收起
is大龙 2020-03-23 23:57:18 633 0
1 条回答
写回答
取消 提交回答
  • 只需将打印总和带出for循环,以便在计算最终分数之前将所有字符串都转换为整数

    def __init__(self):
    #assign loneliness-scores to the various user-response
         for i in range(len(df_array)):
             if df_array[i] == 'Often':
                 df_array[i] = 3
             elif df_array[i] == 'Sometimes':
                 df_array[i] = 2
             elif df_array[i] == 'Rarely':
                 df_array[i] = 1
             elif df_array[i] == 'Never':
                  df_array[i] = 0
         #obtain sum of values for final scoring
         print(sum(df_array))
    

    回答来源:stackoverflow

    2020-03-23 23:57:24
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载