0

Sorry, can you help me? I'm trying to calculate the values for the same key, exported form csv file:

dd = {}

with open('input_1.csv') as File:

    reader = csv.reader(File, delimiter=';')
    g=list(reader)
    for key,value in g.items():
        key = row[0]
        value = int(row[1])
        if key in reader:
            dd[key] += value

print(dd)

It comes out:

  File "C:/Users/User/Documents/Visual Studio 2017/DjangoWebProject1/DjangoWebProject1/app/6_8.py", line 20, in <module>
    for key,value in g.items():
AttributeError: 'list' object has no attribute 'items'
  • 1
    it's the `dict()` not the `list()` that contains attributes `items`,however it's not clear what you trying to achieve,could you give us an example? – Shubham Shaswat Jun 02 '20 at 11:40

1 Answers1

0

When you do g=list(reader) you're reading the values into a list. Later, you treat it as if it were a dictionary.

for key,value in g.items(): would work if g was a dictionary instead of a list. The csv library doesn't seem to provide an easy helper method to read it as a dictionary instead, but another stackoverflow answer can help you there: Creating a dictionary from a CSV file

Nyubis
  • 528
  • 5
  • 12