使用str.split(),str.eeplace(),str.lower(),len(##)
使用encoding="utf-8"避免乱码
直接改动原来的文件read.txt
这不难
第一题
with open('yzy.txt','r') as f:
p = f.readlines()
a = []
for i in p:
a += i.strip().replace('.', '').lower().split()
s = f'\n{len(p)} {len(set(a))}'
with open('yzy.txt','a') as f:
f.write(s)
第二题
with open('yzy.txt','r',encoding='utf-8') as f:
p = f.read()
print(p)
第三题
with open('read.txt','r') as f:
p = f.read()
s = p.replace('world','python')
with open('read.txt','w') as f:
f.write(s)
第四题
s = '\n2021年5月'
with open('yzy.txt','a',encoding='utf-8') as f:
f.write(s)