zoukankan      html  css  js  c++  java
  • day8 homework

    1. 文件a.txt内容:每一行内容分别为商品名字,价钱,个数。

    apple 10 3

    tesla 100000 1

    mac 3000 2

    lenovo 30000 3

    chicken 10 3

    通过代码,将其构建成这种数据类型:[{'name':'apple','price':10,'amount':3},{'name':'tesla','price':1000000,'amount':1}......] 并计算出总价钱。

    li=[]
    name_list=['name','price','amount']
    with open('a.txt',encoding='utf-8',mode='r+') as f1:
        for i in f1:
            l2=i.strip().split()
            # l2=[apple,10,3,2004]
            dic ={}     #字典要在循环中创建,否则会被覆盖
            for j in range(len(l2)):
                dic[name_list[j]] = l2[j]
            li.append(dic)
    print(li)
    sum = 0
    for i in li:
        print(i)
        sum1=int(i['price'])*int(i['amount'])
        sum = sum +sum1
    print(sum)

    2,有如下文件:

    -------

    alex是老男孩python发起人,创建人。

    alex其实是人妖。

    谁说alex是sb?

    你们真逗,alex再牛逼,也掩饰不住资深屌丝的气质。

    ----------

    将文件中所有的alex都替换成大写的SB

    with open('log',encoding='utf-8') as f1,
        open('log_back',encoding='utf-8',mode='w') as f2:
        content = f1.read()
        content = content.replace('alex','SB')
        print(content)
    麻烦占空间
    import
    os with open('log',encoding='utf-8') as f1, open('log_back',encoding ='utf-8',mode='w') as f2: content = f1.read() new_content = content.replace('alex','SB') f2.write(new_content) os.remove('log') os.rename('log_back','log')
    # two
    import os
    
    with open('log',encoding='utf-8')as f1,
            open('log.bak',encoding='utf-8',mode='w') as f2:
        for i in f1:
            new_i=i.replace('SB','WUSIR')
            f2.write(new_i)
    os.remove('log')
    os.rename('log.bak','log')

    3. 文件a1.txt内容:每一行内容分别为商品名字,价钱,个数。
    文件内容:

    name:apple price:10 amount:3 year:2012
    name:tesla price:100000 amount:1 year:2013


    通过代码,将其构建成这种数据类型:
    [{'name':'apple','price':10,'amount':3},
    {'name':'tesla','price':1000000,'amount':1}......]
    并计算出总价钱。

    l1=[]
    with open('a1.txt',encoding = 'utf-8')as f1:
    for i in f1:
    li=i.strip().split()
    dic = {}
    for j in li:
    l2=j.strip().split(':')
    dic[l2[0]]=l2[1]
    l1.append(dic)
    print(l1)
    sum=0
    for i in l1:
    sum1 =int(i['price'])*int(i['amount'])
    sum+=sum1
    print(sum)

    4,文件a2.txt内容:每一行内容分别为商品名字,价钱,个数。

    文件内容:
    序号     部门      人数      平均年龄      备注
    1       python    30         26         单身狗
    2       Linux     26         30         没对象
    3       运营部     20         24         女生多
    通过代码,将其构建成这种数据类型:
    [{'序号':'1','部门':Python,'人数':30,'平均年龄':26,'备注':'单身狗'},
    ......]
    并计算出总价钱。

    l1=[]
    
    with open('a2.txt',encoding='utf-8')as f1:
        list_name=f1.readline().strip().split()
        for i in f1:
            l2=i.strip().split()
            dic={}
            for j in range(len(l2)):
                dic[list_name[j]]=l2[j]
            l1.append(dic)
    print(l1)
  • 相关阅读:
    命名是个技术活(转)
    我想知道的是这个月哪种商品销售量最高,比上个月怎么样?销量近几个月的走势是什么?有没有未达标的?有没有超额完成的?超额完成了多少?我可不关心这个月到底售出了多少件,几点售出的,谁买的(转)
    一个demo
    oracle 创建字段自增长——两种实现方式汇总(转)
    Ruby 一些经常使用的细节
    CMap与hash_map效率对照
    Unity 4.5.2 for Mac 下载+安装+破解
    Android中View绘制流程以及invalidate()等相关方法分析
    Android Studio 100 tips and tricks
    JavaScript-2.2 document.write 输出到页面的内容
  • 原文地址:https://www.cnblogs.com/kateli/p/8659963.html
Copyright © 2011-2022 走看看