要把统计结果写入文件中,可以使用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"中。