gpt4 book ai didi

python - 类型错误 : argument of type 'file' is not iterable 2. 7 python

转载 作者:行者123 更新时间:2023-11-30 22:51:49 35 4
gpt4 key购买 nike

我正在尝试创建现有字典并将其写入 csv 文件,但我不断收到此错误,但我无法弄清楚:

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
>>>

这是给我错误的代码:

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"

csv 文件当前包含以下信息:产品安塔尔233 60第2234章 1

最佳答案

第 21 行将写入为 csvfile,而不是in csvfile

Python 中的 in 关键字后跟一个可迭代对象,这就是您收到该错误消息的原因。

关于python - 类型错误 : argument of type 'file' is not iterable 2. 7 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38884380/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com