要把統計結果寫入文件中,可以使用Python中的文件操作函數。以下是一種方法:
statistics = {
"apple": 10,
"banana": 5,
"orange": 8
}
2.接下來,打開一個文件對象并將統計結果寫入文件中:
with open("statistics.txt", "w") as file:
for key, value in statistics.items():
file.write(f"{key}: {value}\n")
這段代碼將打開一個名為"statistics.txt"的文件,并將統計結果逐行寫入文件中。
file.close()
這樣就把統計結果成功寫入到了文件"statistics.txt"中。