溫馨提示×

Python怎么把統計結果寫到文件中

小億
119
2024-06-03 18:30:08
欄目: 編程語言

要把統計結果寫入文件中,可以使用Python中的文件操作函數。以下是一種方法:

  1. 首先,將統計結果存儲在一個字典中,如下所示:
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"的文件,并將統計結果逐行寫入文件中。

  1. 最后,關閉文件對象:
file.close()

這樣就把統計結果成功寫入到了文件"statistics.txt"中。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女