zoukankan      html  css  js  c++  java
  • 327 作业

    .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}......] 并计算出总价钱。

    f = open('a.txt.py',encoding='utf-8')
    a={}
    b=[]
    c=0
    for i in f:
    li=i.split()
    a['name']=li[0]
    a['price']=li[1]
    a['amount'] = li[2]
    d=int(a['price'])
    c+=d
    b.append(a)
    print(b)
    print(c)
    f.close()

    2,有如下文件:

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

    alex其实是人妖。

    谁说alex是sb?

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

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

    import  os
    with open('a.txt.py',encoding='utf-8')as f1,
        open('a1.txt.py','w',encoding='utf-8')as f2:
        for i in f1:
            new_i=i.replace('alex','SB')
            f2.write(new_i)
    os.remove('a.txt.py')
    os.rename('a1.txt.py','a.txt.py')文件a1.txt内容:每一行内容分别为商品名字,价钱,个数。

    3.文件内容

    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}......]

    f = open('a.txt.py',encoding='utf-8')
    b=[]
    for i in f:
        c = {}
        a=i.split()
        for j in a:
            j=j.split(':')
            c.setdefault(j[0],j[1])
        b.append(c)
    d=int(b[0]['amount'])*int(b[0]['price'])+int(b[1]['amount'])*int(b[1]['price'])
    print(b)
    print(d)
    f.close()
  • 相关阅读:
    透明数据加密 (TDE)常见问题解答
    oracle wallet使用与维护
    Mybatis 一对一、一对多、多对一
    Mybatis-Plus
    eclipse安装spring boot插件spring tool suite
    springboot在idea实现热部署
    springboot在eclipse实现热部署
    SpringBoot配置文件-application.properties详解
    Dubbo入门
    Shell入门
  • 原文地址:https://www.cnblogs.com/Mr-Murray/p/8660528.html
Copyright © 2011-2022 走看看