exchangerates = []
newrate = float(input("Enter amount:"))
with open("ExchangeRates.txt","r") as readfile:
for line in readfile:
exchangerates.append(line.strip().split(","))
for line in exchangerates:
if line[0] == currency1 and line[1] == currency2:
line[2] = newrate
print("The exchange rate for",currency1,"and",currency2,"has been changed to",newrate)
next
return 0
此代码有效,但我需要知道如何将上面创建的列表中的信息传递到文件中,显示为“Exchangerates.txt”。我将如何做到这一点?如何将列表的内容传递给现有文件?
样本输入和输出:
输入货币1:日元
输入货币2:美元
输入量:3
用于日元与美元的汇率已被更改为3.0
这是发生在我的代码,这是预期的。但是它不会写入文件,并且文件中的信息没有更改,这是我的问题。
目前尚不清楚您所期望的输出是什么;你能提供一些样本输出吗? – 2014-08-31 18:07:20
@MartijnPieters:如果当前行包含currency1和currency2,OP看起来像是用新汇率写入相同的csv文件(从中读取)。 – shahkalpesh 2014-08-31 18:17:11
听起来像你的典型[CSV替换作业](http://stackoverflow.com/search?q=python+replace+csv),的确如此。 – 2014-08-31 18:22:58