# -*- coding: utf-8 -*-
# @Time : 2018/12/27 17:27
# @Author : Endless-cloud
# @Site :
# @File : day 8课后作业.py
# @Software: PyCharm
'''
# 1.有如下文件,a1.txt,里面的内容为:
# 老男孩是最好的学校,
# 全心全意为学生服务,
# 只为学生未来,不为牟利。
# 我说的都是真的。哈哈
# 分别完成以下的功能:
# a,将原文件全部读出来并打印。
# b,在原文件后面追加一行内容:信不信由你,反正我信了。
# c,将原文件全部读出来,并在后面添加一行内容:信不信由你,反正我信了。
# d,将原文件全部清空,换成下面的内容:
# 每天坚持一点,
# 每天努力一点,
# 每天多思考一点,
# 慢慢你会发现,
# 你的进步越来越大。
'''
# with open('f:/a.txt','r',encoding='utf-8') as f:
# l =f.read()
# print(l)
# with open('f:/a.txt', 'a', encoding='utf-8') as f:
# f.write('信不信由你,反正我信了。')
# with open('f:/a.txt', 'r+', encoding='utf-8') as f:
# l =f.read()
# f.write('信不信由你,反正我信了。')
# print(l)
# l ='''每天坚持一点,
# 每天努力一点,
# 每天多思考一点,
# 慢慢你会发现,
# 你的进步越来越大。'''
# with open('f:/a.txt', 'w', encoding='utf-8') as f:
# f.write(l)
# s2 = f.writelines(l)
# print(s2)
'''
# 2.有如下文件,t1.txt,里面的内容为:
# 葫芦娃,葫芦娃,
# 一根藤上七个瓜
# 风吹雨打,都不怕,
# 啦啦啦啦。
# 我可以算命,而且算的特别准:
# 上面的内容你肯定是心里默唱出来的,对不对?哈哈
# 分别完成下面的功能:
# a,以r+的模式打开原文件,判断原文件是否可读,是否可写。
# b,以r的模式打开原文件,利用for循环遍历文件句柄。
# c,以r的模式打开原文件,以readlines()方法读取出来,并循环遍历 readlines(),
并分析b,与c 有什么区别?深入理解文件句柄与 readlines()结果的区别。
# d,以r模式读取‘葫芦娃,’前四个字符。
# e,以r模式读取第一行内容,并去除此行前后的空格,制表符,换行符。
# f,以r模式打开文件,从‘风吹雨打.....’开始读取,一直读到最后。
# g,以a+模式打开文件,先追加一行:‘老男孩教育’然后在从最开始将 原内容全部读取出来。
# h,截取原文件,截取内容:‘葫芦娃,葫芦娃,’
'''
# with open('f:/b.txt','r+',encoding='utf-8') as f:
# if f.readable():
# print('ok')
# with open('f:/b.txt','r',encoding='utf-8') as f:
# for i in f:
# print(i)
# c 跟人感觉没差别
# with open('f:/b.txt','r',encoding='utf-8') as f:
# l = f.readlines()
# for i in l:
# print(i)
# d
# with open('f:/b.txt','r',encoding='utf-8') as f:
# l2 =f.read(4) #读的时候是按照字符读的
#
# print(l2)
# e
# with open('f:/b.txt','r',encoding='utf-8') as f:
# l =f.readline().strip()
# print(l)
# f
# with open('f:/b.txt','r',encoding='utf-8') as f:
# l =f.read().strip()
# # print(l)
# # f.seek()
# s =l.find('风')
# print(l[17:])
# g
# with open('f:/b.txt','a+',encoding='utf-8') as f:
# f.seek(0)
# f.write('老男孩教育')
# f.seek(0)
# l =f.read()
# print(l)
# h 看懂不 为什么不是24 要去28 XXXXXXXXXXXXXXXXXXXXXXXXXXXX
# with open('f:/b.txt','a',encoding='utf-8') as f:
# print(len('葫娃芦,葫芦娃,'))
# l =f.truncate(24)
# print(l)
# 3.文件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}......] 并计算出总价钱。
# [{'name':'apple','price':10,'amount':3},{'name':'tesla','price':1000000,'amount':1}......]
# ^^^^^ 方法1
#
# l3 =[]
# l1 =['name','price','amount']
# dic ={}
# with open('f:/c.txt','r',encoding='utf-8') as f:
# for i in f:
# l =i.strip().split(' ')
# dic =dict(zip(l1,l))
# l3.append(dic)
#
# print(l3)
# 4.有如下文件:
# alex是老男孩python发起人,创建人。
# alex其实是人妖。
# 谁说alex是sb?
# 你们真逗,alex再牛逼,也掩饰不住资深屌丝的气质。
# 将文件中所有的alex都替换成大写的SB(文件的改的操作)。
# with open('f:/d.txt','r+',encoding='utf-8') as f:
# old_l =f.read()
# # print(old_l)
# f.seek(0)
# new_l =old_l.replace('alex','SB')
# # print(new_l)
# f.write(new_l)
import os
# with open('f:/f.txt','r',encoding='utf-8') as f1 ,open('f:/fbak.txt','w',encoding='utf-8')as f2:
# old_ = f1.read()
# new_= old_.replace('alex','SB')
# f2.write(new_)
# os.remove('f.txt')
# os.rename('fbak.txt','f.txt')
# 5.文件a1.txt内容(升级题)
#
# name:apple price:10 amount:3 year:2012
# name:tesla price:100000 amount:1 year:2013
# .......
#
# 通过代码,将其构建成这种数据类型:
# [{'name':'apple','price':10,'amount':3,year:2012},
# {'name':'tesla','price':1000000,'amount':1}......]
# 并计算出总价钱。
lis3 = []
with open('f:/aa.txt', 'r', encoding='utf-8') as f:
for i in f:
l1 = i.strip().split(' ')
dic = {}
for i in l1:
l2 = i.split(':')
dic[l2[0]] = l2[1]
# print(dic)
lis3.append(dic)
print(lis3)
# print(lis3)
# lis3 = []
# dic ={}
# sun =0
# with open('f:/aa.txt','r',encoding='utf-8') as f:
# for i in f:
# l1 =i.strip().split(' ')
#
# for i in l1:
# l2 =i.split(':')
# dic[l2[0]] =l2[1]
# lis3.append(dic)
# sun += int(dic['price'])*int(dic['amount'])
# print(dic['price'],int(dic['amount']))
# dic ={}
# # print()
# print(lis3,sun)
#
# 6.文件a1.txt内容(升级题)
# 序号 部门 人数 平均年龄 备注
# 1 python 30 26 单身狗
# 2 Linux 26 30 没对象
# 3 运营部 20 24 女生多
# .......
#
# 通过代码,将其构建成这种数据类型:
# [{'序号':'1','部门':Python,'人数':30,'平均年龄':26,'备注':'单身狗'},
# ......]
# l5=[]
# lis1=['序号', '部门' , '人数' , '平均年龄' ,'备注']
# l6= []
# dic ={}
# with open('f:/rrr.txt','r',encoding='utf-8') as f:
# l =f.readlines()
# l2 =l[1:]
#
# # print(l[1:])
# for i in l2:
# i =i.split()
# l5.append(i)
# for j in l5:
# l6.append(dict(zip(lis1,j)))
#
# print(l6)
# l =f.readline()
# N1 =f.tell()
# l2 =f.seek(N1+1)
# l3= f.read(62)
# print(l3)