习题2:判断 一个文件中有几行含英文单词
import string
count=0
with open('e:\\test\\333.txt','r',encoding='utf-8')as fp:
for line in fp:
for word in line:
if word in string.ascii_letters:
count+=1
break
print(count)
#2
import string
count=0
with open('e:\\test\\333.txt','r',encoding='utf-8')as fp:
content=fp.readlines()
for line in content:
for word in line:
if word in string.ascii_letters:
count+=1
break
print(count)
习题3:以r+方式,把一句话追加到文件末尾,然后读出所有文件
with open('e:\\test\\333.txt','r+',encoding='utf-8')as fp:
content=fp.readlines()
fp.write('lalalal')
fp.seek(0,0)
content=fp.read()
print(content)
习题4:把一个文件内容中的boy替换成xxx
with open('e:\\test\\333.txt','r+',encoding='utf-8')as fp:
contents=fp.readlines()
print(contents)
fp.seek(0,0)
for line in contents:
fp.write(line.replace('boy','xxx'))
fp.seek(0,0)
print(fp.read())
#2
with open('e:\\test\\333.txt','r+',encoding='utf-8')as fp:
content=fp.read()
content=content.replace('boy','xxx')
fp.seek(0,0)
fp.write(content)
习题4:一个文件拆分成5个文件
file_obj1 = open("e:\\test\\333.txt","r",encoding="utf-8")
file_no = 1 #文件名称的序号名
file_line = 0 #记录文件行数
backup_content = "" #要写的内容
for line in file_obj1: #遍历文件
file_line += 1
backup_content += line
if file_line %2 ==0:
with open("e:\\test"+str(file_no)+".txt","w",encoding="utf-8") as file_obj2:
file_obj2.write(backup_content)
file_no += 1
backup_content = "" #初始化backup_content
习题5:过滤掉一个文件中空行
with open('e:\\test\\333.txt','r',encoding='utf-8')as fp:
content=fp.readlines()
print(content)
for line in content:
if line=='\n':
continue
else:
with open('e:\\test\\444.txt','a',encoding='utf-8')as fp:
fp.write(line)
#2
with open('e:\\test\\333.txt','r',encoding='utf-8')as fp:
for line in fp:
if line.strip()=='':
continue
else:
with open('e:\\test\\444.txt','a',encoding='utf-8')as fp:
fp.write(line)
#3
with open('e:\\test\\333.txt','r',encoding='utf-8')as fp:
content=fp.readlines()
print(content)
new_content=[]
for line in content:
if line=='\n':
continue
else:
new_content.append(line)
with open('e:\\test\\333.txt','w',encoding='utf-8')as fp:
fp.writelines(new_content)
习题6:处理数据文件
数据文件:data.log
20160215000148|0|collect info job pos|success|
20160215000153|0|collect info job end|success|resultcode = 0000
20160216000120|0|collect info job pos|success|
20160216000121|0|collect info job end|success|resultcode = 0000
20160217000139|0|collect info job pos|success|
20160217000143|0|collect info job end|success|resultcode = 0000
数据分析需求:
每行内容需要生成以每行首年月日为名称的文件,文件内容写入|0|后的所有行内容(也包括|0| )
with open('e:\\test\\333.txt','r',encoding='utf-8')as fp:
for line in fp:
file_name=line[:14]
file_content=line[14:]
with open('e:\\'+file_name+'.txt','w',encoding='utf-8')as fp:
fp.write(file_content)
习题7:删除一个目录下文件
import os
import os.path
os.chdir('e:\\test')
for i in os.listdir('e:\\test'):
if os.path.isfile(i):
os.remove(i)
#2
import os
os.chdir('e:\\test')
for root,dirs,files in os.walk('e:\\test'):
for file in files:
if os.path.isfile(file):
os.remove(file)
习题8:文件中有两行内容,在中间再加入一行
with open('e:\\test\\333.txt','r+',encoding='utf-8')as fp:
content=fp.readlines()
insert_content='hhhhh\n'
new_content=content[0]+insert_content+content[-1]
with open('e:\\test\\333.txt','w',encoding='utf-8')as fp:
fp.write(new_content)
#2
with open('e:\\test\\333.txt','r+',encoding='utf-8')as fp:
content=fp.readlines()
content.insert(1,'hhhh\n')
with open('e:\\test\\333.txt','w',encoding='utf-8')as fp:
fp.writelines(content)
python练习2020/10/17
最新推荐文章于 2024-10-05 08:57:27 发布