Error object is not subscriptable

0

Hello Please, I want to write 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 can be? Is a program in Python 3

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:])
    
asked by anonymous 22.08.2017 / 22:58

1 answer

0

You are using brackets in open when you should be using braces.

Instead of:

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

Try:

with open('tabela.csv', 'a') as f:
    
22.08.2017 / 23:11