【问题标题】:I am trying to read from a .txt file then write to .dat file with python我正在尝试从 .txt 文件中读取,然后使用 python 写入 .dat 文件
【发布时间】:2022-06-16 01:46:23
【问题描述】:
def writeToFile():
readFile = open("ItemsPurchased.txt", "r")
itemCounter = {}
for purchased_item in file.read().split():
    if purchased_item not in itemCounter:
        itemCounter[purchased_item] = 1
    else:
        itemCounter[purchased_item] += 1
readFile.close()
writeFile = open("frequency.dat", 'w')
for key, value in itemCounter.items():
    writeFile.write("{} {}" .format(key, value))

程序不会创建信息并将其写入文件。我已经使用谷歌几个小时了。我只需要新文件来显示购买的商品和出现的次数。

【问题讨论】:

  • 您的代码没有正确缩进

标签: python file


猜你喜欢
  • 2023-04-02
  • 2021-06-10
  • 1970-01-01
  • 2021-07-15
  • 1970-01-01
  • 2011-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多