1 Star 0 Fork 0

Python28/yl

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
day06.py 4.58 KB
Copy Edit Raw Blame History
杨礼 authored 2019-12-06 09:46 . 重命名 day07.py 为 day06.py
# 1.有如下文件,a1.txt,里面的内容为:
#
# 老男孩是最好的学校,
# 全心全意为学生服务,
# 只为学生未来,不为牟利。
# 我说的都是真的。哈哈
# 分别完成以下的功能:
# a,将原文件全部读出来并打印。
# with open("a1.txt","r",encoding="utf-8")as f:
# print(f.read())
# b,在原文件后面追加一行内容:信不信由你,反正我信了。
# with open("a1.txt","a",encoding="utf-8")as f:
# print(f.write("信不信由你,反正我信了。"))
# c,将原文件全部清空,换成下面的内容:
# 每天坚持一点,
# 每天努力一点,
# 每天多思考一点,
# 慢慢你会发现,
# 你的进步越来越大。
# with open("a1.txt","w",encoding="utf-8")as f:
# print(f.write("每天坚持一点,\n每天努力一点,\n每天多思考一点,\n慢慢你会发现,\n你的进步越来越大。\n"))
# 2.有如下文件,t1.txt,里面的内容为:
#
# 葫芦娃,葫芦娃,
# 一根藤上七个瓜
# 风吹雨打,都不怕,
# 啦啦啦啦。
# 我可以算命,而且算的特别准:
# 上面的内容你肯定是心里默唱出来的,对不对?哈哈
# 分别完成下面的功能:
# a,以r的模式打开原文件,利用for循环遍历文件句柄。
# with open("t1.txt","r",encoding="utf-8")as f:
# for i in f:
# print(i)
# b,以r模式读取‘葫芦娃,’前四个字符。
# with open("t1.txt","r",encoding="utf-8")as f:
# print(f.read(4))
# c,以r模式读取第一行内容,并去除此行前后的空格,制表符,换行符。
# with open("t1.txt","r",encoding="utf-8")as f:
# print(f.readline().strip())
# d,以a+模式打开文件,先追加一行:‘老男孩教育’然后在从最开始将原内容全部读取出来。
# with open("t1.txt","a+",encoding="utf-8")as f:
# print(f.write("老男孩教育’然后在从最开始将原内容全部读取出来。"))
# 3.文件a.txt内容:每一行内容分别为商品名字,价钱,个数。 (20分钟)
#
# 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}......] 并计算出总价钱。
# a = []
# with open("a.txt","r",encoding="utf-8")as f:
# for i in f:
# i = i.strip().strip().split(" ")
# b = {"name":i[0],"price":i[1],"amount":i[2]}
# a.append(b)
# print(a)
# sum = 0
# for j in a:
# sum += int(j.get('amount')) * int(j.get("price"))
# print(sum)
# 4.有如下文件:
#
# alex是老男孩python发起人,创建人。
# alex其实是人妖。
# 谁说alex是sb?
# 你们真逗,alex再牛逼,也掩饰不住资深屌丝的气质。
# 将文件中所有的alex都替换成大写的SB(文件的改的操作)。
# import os
# with open("aa.txt","r",encoding="utf-8")as f,\
# open("bb.txt","w",encoding="utf-8")as f1:
# for i in f:
# i = i.replace("alex","SB")
# f1.write(i)
# os.replace("aa.txt","aa.bf")
# os.replace("bb.txt","aa.txt")
#
# 5.文件a1.txt内容,建议写的支持扩展 (35分钟)
#
# 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}]
# 并计算出总价钱。
# b = []
# with open("a1.txt","r",encoding="utf-8")as f:
# for i in f:
# a={}
# i = i.strip().split(" ")
# # print(i)
# for j in i:
# f,v = j.split(":")
# a[f] = v
# # print(a)
# b.append(a)
# print(b)
# sum = 0
# for o in b:
# sum += int(o.get("price"))*int(o.get("amount"))
# print(sum)
# a = {i[0]:i[1]}
# print(a)
# 6.文件a1.txt内容,建议写的支持扩展 (40分钟)
#
# 序号 部门 人数 平均年龄 备注
# 1 python 30 26 单身狗
# 2 Linux 26 30 没对象
# 3 运营部 20 24 女生多
# 通过代码,将其构建成这种数据类型:
# [{'序号':'1','部门':Python,'人数':30,'平均年龄':26,'备注':'单身狗'},
# ......]
# p = []
# with open("a1.txt","r",encoding="utf-8")as f:
# o = f.readline()
# a,b,c,d,e = o.split()
# for i in f:
# a1, a2, a3, a4, a5=i.split()
# m = {a:a1,b:a2,c:a3,d:a4,e:a5}
# p.append(m)
# print(p)
#
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/old_boy_education_python_28/yl.git
git@gitee.com:old_boy_education_python_28/yl.git
old_boy_education_python_28
yl
yl
master

Search