python如何打印txt文件_如何在python中打印文本文件中的所有行?

我正在尝试以某种格式从文本文件中打印每行文本,但它只打印最后一行而不是所有行。我正在使用的文本文件有4行,但根据用户可能有更少或更多。

我的代码:def loadrecipefile (recipe_file):

infile=open(recipe_file)

Linelist=infile.readlines()

global cookbook

for line in Linelist:

wordList=line.split()

r1={'apple':int(wordList[1]),'beets':int(wordList[2]),'carrots':int(wordList[3])}

cookbook={wordList[0]:r1}

def printrecipes():

for name,ingred in cookbook.items():

print(name + " " + str(ingred['apple']) + " " + str(ingred['beets']) + " " + str(ingred['carrots']))

所以输入将是(在我的情况下):loadrecipefile("recipe_file.txt")

printrecipes()

那么它会打印我希望它看起来像文本文件的每一行:Recipe1 1 4 3

Recipe2 0 2 4

Recipe3 3 0 1

Recipe4 2 1 0

但我只得到最后一行: Recipe4 2 1 0

我不知道如何解决这个问题,因为loadrecipe文件似乎工作得很好jsut printcipes没有打印出我想要的东西

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值