TypeError: list indices must be integers or slices, not str

0

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

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"
      ],
    
asked by anonymous 30.10.2018 / 22:55

1 answer

0

I'll have to use my crystal ball, because a major part of the code is missing, and the error message is incomplete.

In this part of the code:

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

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

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

If you are within for , I believe you wanted to access the for variable that was called infos .

Try to do this:

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

Or simplifying more, it may look like this:

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

If this does not resolve, edit the question and put relevant parts of the pagarme module in addition to the full error message, which I'll edit my response later.

    
30.10.2018 / 23:25