我正在尝试编写一个带有代码和csv文件的货币转换器,其中可以编辑csv文件以节省使用之间的新汇率。但是我在写入文件并更改速率时遇到问题:
def change():
print()
print()
print()
print("1) Change existing exchange rate.")
print("2) Add new exchange rates.")
print("3) Quit.")
exRtFile = open ('exchangeRate.csv')
exchReader = csv.reader(exRtFile)
exchWriter = csv.writer(exRtFile)
loop2=0
while loop2==0:
selected=int(input("Please select an option: "))
if selected == 1:
change = input("What rate would you like to change: ")
changeRt = float(input("What would you like to change the rate to: "))
for row in exchReader:
currency = row[0]
if currency == change:
crntRt = row[1]
crntRt = changeRt
exchWriter.writerows(crntRt)
继承人和输入/输出的例子:
1) Change existing exchange rate.
2) Add new exchange rates.
3) Quit.
Please select an option: 1
What rate would you like to change: Euro
What would you like to change the rate to: 1.23
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
converter()
File "py", line 21, in converter
change()
File "py", line 91, in change
exchWriter.writerows(crntRt)
TypeError: writerows() argument must be iterable
答案 0 :(得分:0)
writeRows()
,就其本质而言,期望一个变量包含一堆值,每个值包含要写入输出文件中一行的数据。
因为您提供writerows()
一个float
值,所以它通过告诉您它不知道如何将其转换为多行来进行响应 - 在&#34 ; Python说&#34; must be iterable
表示&#34;我无法计算如何从您给我的项目中获取多个值&#34;。
您的代码中可能还有许多其他问题。特别是:
crntRt = row[1]
crntRt = changeRt
exchWriter.writerows(crntRt)
让我觉得你在某种程度上期望在你的输出中看到row[1]
的值,但是你不会因为下一行用新的汇率覆盖该值。