【发布时间】:2014-04-09 11:56:52
【问题描述】:
我正在尝试编写一个货币转换器,其中包含代码和一个 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
【问题讨论】:
-
在不知道输入的情况下,很难调试它。你能提供一个工作的例子吗?但是这个错误是不言自明的:要么使用
writerow(注意单数;你只想在循环中写一行,对吧?)或者将一个可迭代对象传递给writerows-方法。