- 学习测试开发的Day102,真棒!
- 学习时间为1H
- 第十次全天课(上午视频0M-12M+2道习题)
题1.把一个文件中包含数字的所有行,筛选存到一个新文件中
自己的代码:
fp=open("D:\\0grory\\day10\\num.txt","r")
lines=fp.readlines()
content=""
for l in lines:
flag=0
for i in l:
if i.isdigit():
flag=1
if flag==1:
content+=l
content+="\n"
outfp=open("D:\\0grory\\day10\\numnew.txt","w")
outfp.write(content)
outfp.close()
fp.close()
执行
PS D:\0grory\day10> python .\choicenum.py
PS D:\0grory\day10>
结果:
老师的代码:
fp1.open("D:\\0grory\\day10\\num.txt","r")
fp1.open("D:\\0grory\\day10\\numt2.txt","w")
for i in fp1:
for j in i:
if j in "0123456789":
fp2.write(i)
break
fp1.close()
fp2.close()
执行:
PS D:\0grory\day10> python .\choicenum.py
PS D:\0grory\day10>
结果:
题2.把一个文件的所有行倒序输出,倒序输出后存到一个新文件中。
自己的代码:
fp=open("D:\\0grory\\day10\\1.txt","r")
lines=fp.readlines()
str=""
for l in lines:
str=l+str
print(str)
fp2=open("D:\\0grory\\day10\\2.txt","w")
fp2.write(str)
fp.close()
fp2.close()
执行:
PS D:\0grory\day10> python .\daoxu.py
1111
2222222
333333
44444
55555
结果:
老师的代码:
fp=open("D:\\0grory\\day10\\1.txt","r")
fp2=open("D:\\0grory\\day10\\2.txt","w")
for line in fp1.readlines()[::-1]:
fp2.write(line)
fp.close()
fp2.close()
执行:
PS D:\0grory\day10> python .\daoxu.py
结果: