Danny Bolland
Danny Bolland

Reputation: 33

TypeError: argument of type 'file' is not iterable 2.7 python

I'm trying to create and write an existing dictionary to a csv file but I keep getting this error which I can't figure out:

Traceback (most recent call last):
  File "/Users/Danny/Desktop/Inventaris.py", line 52, in <module>
    Toevoeging_methode_plus(toevoegproduct, aantaltoevoeging)
  File "/Users/Danny/Desktop/Inventaris.py", line 9, in Toevoeging_methode_plus
    writecolumbs()
  File "/Users/Danny/Desktop/Inventaris.py", line 21, in writecolumbs
    with open("variabelen.csv", "wb") in csvfile:
TypeError: argument of type 'file' is not iterable
>>> 

And this is the code that gives me the error:

import csv
import os

def Toevoeging_methode_plus(key_to_find, definition):
    if key_to_find in a:
        current = a[key_to_find]
        newval = int(current) + aantaltoevoeging
        a[key_to_find] = int(current) + aantaltoevoeging
        writecolumbs()


def Toevoeging_methode_minus(key_to_find, definition):
    if key_to_find in a:
        current = a[key_to_find]
        newval = int(current) - aantaltoevoeging
        a[key_to_find] = int(current) - aantaltoevoeging
        writecolumbs()

def writecolumbs():
    os.remove("variabelen.csv")
    with open("variabelen.csv", "wb") in csvfile:
        spamwriter = csv.writer(csvfile, delimiter=' ',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)
        spamwriter.writerow("Product", "Aantal")
        spamwriter.writerows(a)  


a = {}


with open("variabelen.csv") as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        a[row['Product']] = row['Aantal']
        print a

print("""
1. Toevoegen
2. Aftrek
3. Check
""")
askmenu = int(raw_input("Menu Nummer? "))
if askmenu is 1:
    toevoegproduct = raw_input("Productnummer > ")
    aantaltoevoeging = int(raw_input("Hoeveel > "))
    Toevoeging_methode_plus(toevoegproduct, aantaltoevoeging)
    print a

elif askmenu is 2:
    toevoegproduct = raw_input("Productnummer > ")
    aantaltoevoeging = int(raw_input("Hoeveel > "))
    Toevoeging_methode_minus(toevoegproduct, aantaltoevoeging)
    writecolumbs(toevoegproduct, newval)
    print a

elif askmenu is 3:
    checknummer = raw_input("Productnummer > ")
    if checknummer in a:
        print a[checknummer]

else:
    print "oops"

The csv file contains this information currently: Product Aantal 233 60 2234 1

Upvotes: 1

Views: 1449

Answers (1)

user357269
user357269

Reputation: 1913

Write as csvfile not in csvfile on line 21.

The in keyword in python is followed by an iterable, that's why you're getting that error message.

Upvotes: 3

Related Questions