Object is not subscriptable error

Asked

Viewed 3,370 times

2

Hello Please, I want to record a csv file from a list, but this error appears:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-48f68d01456e> in <module>()
----> 1 with open['tabela.csv', 'a'] as f:
      2     # usa a ferramenta "writer" do módulo csv
      3     writer = csv.writer(f)
      4     # se precisarmos gravar o cabeçalho...
      5     if header is True:

TypeError: 'builtin_function_or_method' object is not subscriptable

Does anyone know what it might be? It’s a Python 3 program

The code is this:

import csv

tabela_final = [['Gabinete', 'Periodo', 'Grupo', 'Total'],
 [13493, '04-2015', b'ALMOXARIFADO', b'0,00'],
 [13493, '04-2015', b'COMBUST\xc3\x8dVEIS', b'1.624,11'],
 [13493, '04-2015', b'CORRESPOND\xc3\x8aNCIA / TELEGRAMA', b'251,10'],
 [13493, '04-2015', b'GR\xc3\x81FICA', b'318,50'],
 [13493, '04-2015', b'INSCRI\xc3\x87\xc3\x83O', b'0,00'],
 [13493, '04-2015', b'PASSAGENS', b'241,59'],
 [13493, '04-2015', b'TELEFONE', b'1.974,96'],
 [13493,
  '04-2015',
  b'VERBA INDENIZAT\xc3\x93RIA DO EXERC\xc3\x8dCIO PARLAMENTAR',
  b'759,21']]

header = True

with open['tabela.csv', 'a'] as f:
    # usa a ferramenta "writer" do módulo csv
    writer = csv.writer(f)
    # se precisarmos gravar o cabeçalho...
    if header is True:
        # gravamos o cabeçalho
        writer.writerow(tabela_final[0])
        # marcamos que não é mais preciso gravar o cabeçalho
        header = False
    # gravamos o restante das linhas no CSV
    writer.writerows(tabela_final[1:])

1 answer

1


You’re wearing brackets on open when you should be wearing parentheses.

Instead of :

with open['tabela.csv', 'a'] as f:

Try:

with open('tabela.csv', 'a') as f:

Browser other questions tagged

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