1. 文件a.txt内容:每一行内容分别为商品名字,价钱,个数,求出本次购物花费的总钱数
apple 10 3
tesla 100000 1
mac 3000 2
lenovo 30000 3
chicken 10 3

 sum = 0
with open('a.txt',mode='w',encoding='utf-8') as f:
f.write('''apple 10 3
tasla 100000 1
mac 3000 2
lenove 30000 3
chicken 10 3''')
with open('a.txt',encoding='utf-8') as read_f:
for line in read_f.readlines():
sum += int(line.split(' ')[1])*int(line.split(' ')[2])
print(sum)

2. 修改文件内容,把文件中的alex都替换成SB

 import os
with open('a.txt',mode='w',encoding='utf-8') as f:
f.write('''alex 18
Eva 20
KID 21
kidd 22
alex 38''')
with open('a.txt',encoding='utf-8') as read_f,\
open('aa.txt',mode='w',encoding='utf-8') as write_f:
for line in read_f.readlines():
line = line.replace('alex','SB')
write_f.write(line)
os.remove('a.txt')
os.rename('aa.txt','a.txt')

python学习之老男孩python全栈第九期_day008作业的更多相关文章

  1. python学习之老男孩python全栈第九期_day002作业

    1. 判断下列逻辑语句的True,False.(1) 1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6Tru ...

  2. python学习之老男孩python全栈第九期_day015作业_老男孩Python全9期练习题(面试真题模拟)

    一. 选择题(32分) 1. python不支持的数据类型有:AA. charB. intC. floatD. list 2. Ex = ‘foo’y = 2print(x + y)A. fooB. ...

  3. python学习之老男孩python全栈第九期_day007作业

    一.关系运算 有如下两个集合,pythons是报名python课程的学员名字集合,linuxs是报名linux课程的学员名字集合pythons={'alex','egon','yuanhao','wu ...

  4. python学习之老男孩python全栈第九期_day016作业

    1. 请利用filter()过滤出1~100中平方根是整数的数,即结果应该是: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] import math def func( ...

  5. python学习之老男孩python全栈第九期_day001作业

    1.使用while循环输入 1 2 3 4 5 6     8 9 10 count = 0 while count <= 9: count += 1 if count == 7:continu ...

  6. python学习之老男孩python全栈第九期_day004作业

    看代码写结果:1. a=[1,2,3,6,"dfs",100]s=a[-1:]print (s) 结果:[100] 2. s=a[-1:0:-1]print(s) 结果:[100, ...

  7. python学习之老男孩python全栈第九期_day003作业

    1. 有变量name = "aleX leNb" 完成如下操作:(1) 移除 name 变量对应的值两边的空格,并输出处理结果name = ' aleX leNb 'print(n ...

  8. python学习之老男孩python全栈第九期_day014作业

    0. 默写a. 生成器函数获取移动平均值例子: def init(func): def inner(*args,**kwargs): ret = func(*args,**kwargs) ret.__ ...

  9. python学习之老男孩python全栈第九期_day011作业

    1. 编写函数.(函数执行的时间是随机的) import timeimport randomdef random_time(): ''' 执行时间随机的函数 :return: ''' time.sle ...

随机推荐

  1. 跟着刚哥学习Spring框架--Spring容器(二)

    Spring容器 启动Spring容器(实例化容器) -- IOC容器读取Bean配置创建Bean实例之前,必须对它进行实例化(加载启动),这样才可以从容器中获取Bean的实例并使用.  Bean是S ...

  2. mongoose入门

    概述 像Mysql和Mongodb这样的数据库,一般都是在命令行或者工具里面进行操作,如果想在node搭建的服务器上面操作,就必须要利用特殊的模块的.其中操作Mongodb数据库需要用到mongoos ...

  3. Golang 中哪些值是不可以寻址的

    不可以寻址, 指的是不能通过&获得其地址. golang中不能寻址的可以总结为:不可变的,临时结果和不安全的.只要符合其中任何一个条件,它就是不可以寻址的. 具体为: 常量的值. 基本类型值的 ...

  4. Ubutntu安装docker启动报Removed /etc/systemd/system/docker.service.

    Ubutntu安装docker启动报Removed /etc/systemd/system/docker.service.的错误,只需要执行以下三条命令. systemctl unmask docke ...

  5. Python 模块 和 包

    模块 os模块 路径拼接 os.path.join

  6. Linux的mv 命令

    mv 命令的10个例子 1.移动文件 移动文件时需要注意的是文件的源地址和目标地址必须不同.这里有个例子,想要将file_1.txt文件从当前目录移动到其它目录,以/home/pungki/为例,语法 ...

  7. Chapter 3 Phenomenon——24

    My mom was in hysterics, of course. 我的母亲当时是歇斯底里的发疯了. I had to tell her I felt fine at least thirty t ...

  8. Linux之SElinux安全上下文件(1)

    SELinux:Secure Enhanced Linux,是美国国家安全局(NSA=The National Security Agency)和SCC(Secure Computing Courpo ...

  9. LeetCode 169. Majority Element解题方法

    题目: Given an array of size n, find the majority element. The majority element is the element that ap ...

  10. Java NIO系列教程(九) ServerSocketChannel

    Java NIO中的 ServerSocketChannel 是一个可以监听新进来的TCP连接的通道, 就像标准IO中的ServerSocket一样.ServerSocketChannel类在 jav ...