输入文件并使用.format()获取格式化的字符串输出 [英] To input a file and get a formatted string output using .format()

查看:402
本文介绍了输入文件并使用.format()获取格式化的字符串输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

写一个名为 calculate_expenses 的函数,接收一个文件名作为参数。该文件包含关于物品费用的信息。你的函数应该返回一个基于项目名称排序的元组列表。每个元组包括物品的名称和该物品的总费用,如下所示:


请注意,文件的每一行只包含一个项目,并且该项目的购买价格由a逗号。项目或价格之前或之后可能有空格。然后你的函数应该读取这个文件并返回一个元组列表,例如:





  • 元组是根据项目名称排序的,也就是面包出现在牛奶前面的筹码之前。
    总开支是以$和它们在小数点后有两位数的精度。

  • .2f} 为总费用正确创建和格式化字符串。



    到目前为止的代码:

      def calculate_expenses(file_name):
    file_pointer = open(file_name,'r')
    data = file_pointer.readlines()
    list_main = []
    用于数据行:
    name,price = line.strip()。split(',')$ b $ print(name,price)


    $输出:

    $ $ $ $ $ $ $ $ $($'牛奶','2.35')
    ('bre ('牛奶','2.38')
    ('牛奶','2.31')
    ('bread','1.90')

    我无法摆脱空格,

    解决方案

    可以通过以下方式解决:

    <你可以使用.read()或.readline()或者.read()或者.readline()或者.read()函数。 .readlines()
    data = file_pointer.readlines()
    #现在继续您的代码从这里!

    my_dictionary = {}
    用于数据行:
    item,price = line.strip()。split(',')

    my_dictionary k,v in my_dictionary.items()中的[item.strip()] = my_dictionary.get(item.strip(),0)+ float(价格)
    dic = {}
    :$ b格式(round(v,2))

    L =([(k,v)for k,v in dic。 iteritems()])
    L.sort()

    return L


    Write a function named calculate_expenses that receives a filename as argument. The file contains the information about a person's expenses on items. Your function should return a list of tuples sorted based on the name of the items. Each tuple consists of the name of the item and total expense of that item as shown below:

    Notice that each line of the file only includes an item and the purchase price of that item separated by a comma. There may be spaces before or after the item or the price. Then your function should read the file and return a list of tuples such as:

    Notes:

    • Tuples are sorted based on the item names i.e. bread comes before chips which comes before milk.

    • The total expenses are strings which start with a $ and they have two digits of accuracy after the decimal point.

    Hint: Use ${:.2f} to properly create and format strings for the total expenses.

    The code so far:

    def calculate_expenses(file_name):
    file_pointer = open(file_name, 'r')
    data = file_pointer.readlines()
    list_main=[]
    for line in data:
        name, price = line.strip().split(',')
        print (name, price)
    

    Output:

    ('milk', '2.35')
    ('bread ', ' 1.95')
    ('chips ', '    2.54')
    ('milk  ', '    2.38')
    ('milk', '2.31')
    ('bread', '    1.90')
    

    I can't get rid of the spaces and don't know what to do next.

    解决方案

    Can be solved by :

    def calculate_expenses(filename):
        file_pointer = open(filename, 'r')
        # You can use either .read() or .readline() or .readlines()
        data = file_pointer.readlines()
        # NOW CONTINUE YOUR CODE FROM HERE!!!
    
        my_dictionary = {}
        for line in data:
            item, price= line.strip().split(',')
    
            my_dictionary[item.strip()] = my_dictionary.get(item.strip(),0) + float(price)
        dic={}
        for k,v in my_dictionary.items():
            dic[k]='${0:.2f}'.format(round(v,2))
    
        L=([(k,v) for k, v in dic.iteritems()])
        L.sort()
    
        return L
    

    这篇关于输入文件并使用.format()获取格式化的字符串输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆