缪伏 2022-05-29 18:16 采纳率: 66.7%
浏览 44
已结题

Python题求解,四个题,

img


使用str.split(),str.eeplace(),str.lower(),len(##)

img


使用encoding="utf-8"避免乱码

img


直接改动原来的文件read.txt

img

  • 写回答

1条回答 默认 最新

  • 请叫我问哥 Python领域新星创作者 2022-05-29 18:31
    关注

    这不难
    第一题

    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月29日
  • 已采纳回答 5月29日
  • 创建了问题 5月29日