Python入门 Day3-小练习

1.1  第一题

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

  解答:

def test1():
    sp_cart = []
    total_price = 0
    with open('a.txt',encoding='utf-8',mode='r') as f1:
        for line in f1.readlines():
            name,price,amount = line.split()
            Price = int(price)
            Amount = int(amount)
            sp_cart.append({'name':name,'price':Price,'amount':Amount})
            total_price += Price * Amount
    print(sp_cart)
    print('总价:',total_price)

test1()

 

1.2 第二题

将b.txt中所有alex替换成SB

def test2():
    import os
    with open('b.txt', encoding='utf-8', mode='r') as f1,\
        open('b.txt.swap', encoding='utf-8', mode='w') as f2:
        for line in f1:
            new_line = line.replace('alex', 'SB')
            # new_line = line.replace('SB','alex')
            f2.write(new_line)
    os.remove('b.txt')
    os.rename('b.txt.swap','b.txt')

test2()

这里用到的方法,也是系统对文件更改时使用到的方法。

1.3 第三题

题目要求:

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

解答:

def test3():
    list = []
    totle_price = 0
    with open('c.txt', encoding='utf-8') as f1:
        for line in f1:
            dic = {}
            for i in line.split():
                k, v = i.split(':')
                if k in ['price', 'amount']:
                    v = int(v)
                dic[k] = v
            totle_price += dic.get('price') * dic.get('amount')
            list.append(dic)
    print(list)
    print(totle_price)

test3()

 

1.4 第四题

题目要求:

通过代码将以下文件构建成指定数据类型:

序号   部门     人数    平均年龄      备注
1     python    30        26          单身狗
2      Linux     26        30           没对象
3     运营部     20       24            妹子多

 要求格式:

[{'序号':'1','部门':Python,'人数':30,'平均年龄':26,'备注':'单身狗'},
......]

 代码

def test4():
    list = []
    with open('d.txt', encoding='utf-8') as f1:
        ln = 0
        k_list = []
        for line in f1:
            ln += 1
            if ln == 1:
                for k in line.split():
                    k_list.append(k)
            else:
                k, v = k_list, line.split()
                dict = {}
                n = 0
                while n < 5:
                    dict[k[n]] = v[n]
                    # print(dict)
                    n += 1
                list.append(dict)
        print(list)


# test4()
def test41():
    list = []
    with open('d.txt', encoding='utf-8') as f1:
        k_list = f1.readline().split()
        for line in f1:
            k, v = k_list, line.split()
            dict = {}
            n = 0
            while n < 5:
                dict[k[n]] = v[n]
                n += 1
            list.append(dict)
        print(list)

test41()
def test44():
    list = []
    with open('d.txt', encoding='utf-8') as f1:
        # k = [i for i in f1.readline().split()]
        k = f1.readline().split()
        for v in f1:
            list.append({k: v for k, v in zip(k, v.split())})
        print(list)

test44()

 

posted on 2018-04-19 14:37  鸿飞漫天  阅读(163)  评论(0编辑  收藏  举报

导航