[Python] TypeError: tuple indices must be integers, not str

Discussão em 'Python' iniciado por Stack, Março 22, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to pull data from a database and assign them to different lists. This specific error is giving me a lot of trouble "TypeError: tuple indices must be integers, not str" I tried converting it to float and etc, but to no success.

    The code goes as below

    conn=MySQLdb.connect(*details*)
    cursor=conn.cursor()
    ocs={}
    oltv={}
    query="select pool_number, average_credit_score as waocs, average_original_ltv as waoltv from *tablename* where as_of_date= *date*"
    cursor.execute(query)
    result=cursor.fetchall()

    for row in result:
    print(row)
    ocs[row["pool_number"]]=int(row["waocs"])
    oltv[row["pool_number"]]=int(row["waoltv"])


    Sample output of print statement is as follows :

    ('MA3146', 711L, 81L)
    ('MA3147', 679L, 83L)
    ('MA3148', 668L, 86L)


    And this is the exact error I am getting:

    ocs[row["pool_number"]]=int(row["waocs"])
    TypeError: tuple indices must be integers or slices, not str


    How do I solve this error?

    Continue reading...

Compartilhe esta Página