Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with csv
#1
I'm learning progamming, so i start to learn about CSV, but when I tryed to make something with that i noobed.
Basically I make a progam who you can creat a csv file with name, price and importance of a product. But when i try to print a list after that fail.
Code
# Vinicius Perrud

import csv

#def listcreator for creat lists
def listCreat():
    nomedalista = input('Insira um nome para a lista de compras: ')
    p = str(input('Peça: '))
    p = p.split(' ')
    d = str(input('Custo : '))
    d = d.split(' ')
    i = str(input('Importancia: '))
    i = i.split(' ')
    csvfile = open((nomedalista + '.csv'), 'w')
    filewriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
    filewriter.writerow(['Item ', 'Custo ', 'Importância '])
    # filewriter.writerow([p, d, i])
    a = len(p) - 1
    while a >= 0:
        filewriter.writerow([p[a], d[a], i[a]])
        a = a - 1


#def printList for print a list
def printList():
    nome = str(input('Nome da lista: '))
    nome = nome + '.csv'
    myFile = open(nome, 'r')
    reader = csv.reader(myFile)
    for row in reader:
        reader[row] = 'banana'
        print(reader)

listCreat()
printList()
Error
Error:
Traceback (most recent call last): File "/home/perrud/Documentos/Python/CSV Teste/ListManager.py", line 35, in <module> printList() File "/home/perrud/Documentos/Python/CSV Teste/ListManager.py", line 31, in printList reader[row] = 'banana' TypeError: '_csv.reader' object does not support item assignment
Someone know what is wrong??
Reply
#2
You can't write to a reader. The assignment in line 31 is not supported.
If you just want to print out, change the lines 31,32 to:

        print(row)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020