冰淇淋销售报告文件(Python)

2024-10-01 19:15:09 发布

您现在位置:Python中文网/ 问答频道 /正文

编辑11/25/2015

def main():
    outfile=open('icecreams.txt','w')
    sales_report1={'Vanilla':[8580.0,7201.25,8900.0]}
    sales_report2={'Chocolate':[10225.25,9025.0,9505.0]}
    sales_report3={'Rocky Road':[6700.1,5012.45,6011.0]}
    sales_report4={'Cookie Dough':[7901.25,4267.0,7056.5]}
    outfile.write(str(sales_report1)+'\n')
    outfile.write(str(sales_report2)+'\n')
    outfile.write(str(sales_report3)+'\n')
    outfile.write(str(sales_report4)+'\n')
    outfile.close()
    print('Data written to file') 
main()

#read list of icecreams
def main():
    infile=open('icecreams.txt','r')
    sales_report1=str(infile.readline())
    sales_report2=str(infile.readline())
    sales_report3=str(infile.readline())
    sales_report4=str(infile.readline())
    total=sales_report1
    infile.close()
    print(total)
    print(sales_report1,sales_report2,sales_report3,sales_report4)
main()

它打印出这个:

^{pr2}$

不过,我需要把每个冰淇淋的销售额加起来,但是当我打印出总数时,它会把所有的东西都打印在sales_report1中,这是否意味着字典在这种情况下不是一个好主意?在


Tags: readlinemaindefopeninfileoutfilewriteprint

热门问题