Typeerror: list indices must be integers or Slices, not str

Asked

Viewed 14,224 times

0

Hello, everyone. When I run this code I get the following feedback:

Typeerror: list indices must be integers or Slices, not str

How can I fix this?

import pagarme
import psycopg2

pagarme.authentication_key("API_KEY")

transaction = pagarme.transaction.find_all()

#print(transaction)

conn = psycopg2.connect("dbname='DB' user='USER' host='HOST' 
password='PASSWORD'")
insert = "INSERT INTO TABELA (object,status) VALUES"


gravar=[]
for infos in transaction:
gravar.append((
               transaction['object'],
               transaction['status']
            ))
if len(gravar) >0 :
    cur = conn.cursor()
    y = b','.join(cur.mogrify("(%s,%s)", x) for x in gravar)
    comando = insert + y.decode()
    try:
        cur.execute(comando)
        conn.commit()
    except (Exception, psycopg2.DatabaseError) as error:
         print(error)

    cur.close()
     print('Carga Completa')
else:
    conn.close()
    print('Nada a Inserir')

This is the return of JSON:

    [
  {
    "object": "transaction",
    "status": "refused",
    "refuse_reason": "acquirer",
    "status_reason": "acquirer",
    "acquirer_response_code": "1011",
    "acquirer_name": "pagarme",
    "acquirer_id": "5b1eb16fd15fc06256543da4",
    "authorization_code": null,
    "soft_descriptor": null,
    "tid": 59869003,
    "nsu": 59869003,
    "date_created": "2018-10-30T20:57:01.883Z",
    "date_updated": "2018-10-30T20:57:03.655Z",
    "amount": 20520,
    "authorized_amount": 0,
    "paid_amount": 0,
    "refunded_amount": 0,
    "installments": 1,
    "id": 59869003,
    "cost": 0,
    "card_holder_name": "roberto v raymundo",
    "card_last_digits": "5116",
    "card_first_digits": "544731",
    "card_brand": "mastercard",
    "card_pin_mode": null,
    "postback_url": null,
    "payment_method": "credit_card",
    "capture_method": "ecommerce",
    "antifraud_score": null,
    "boleto_url": null,
    "boleto_barcode": null,
    "boleto_expiration_date": null,
    "referer": "api_key",
    "ip": "104.41.14.184",
    "subscription_id": null,
    "phone": null,
    "address": null,
    "customer": {
       "object": "customer",
      "id": 25744747,
      "external_id": "3804",
      "type": "individual",
      "country": "br",
      "document_number": null,
      "document_type": "cpf",
      "name": "Roberto",
      "email": "[email protected]",
      "phone_numbers": [
        "+5511999999999"
      ],
  • Please edit the question and put the complete error, with traceback.. makes it much easier to find the problem. You put only the last line of the error.

1 answer

1


I will have to use my crystal ball, because it is missing important part of the code, and the error message is incomplete.

In that part of the code:

for infos in transaction:
    gravar.append((
                   transaction['object'],
                   transaction['status']
                ))

You’re making a for in the variable transaction (we do not know what it is because you did not include in your question the function code pagarme.transaction.find_all but I believe it’s a list, by deduction).

Next you try to access elements of this variable transaction using string indexing (transaction['object']) I believe that’s your problem.

If you’re inside a for, I believe what you wanted is to access the variable of for which was called infos.

Try to do so:

gravar = []
for infos in transaction:
    gravar.append((infos['object'], infos['status']))

Or by simplifying it further, it can look like this:

gravar = [(infos['object'], infos['status']) for infos in transaction]

If this does not resolve, edit the question and place relevant parts of the module pagarme in addition to the complete error message, which I will edit my reply after.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.