8.3 文件读取的课后练习讲解

练习

  1. 统计文本内,某单词出现的次数

    f = open("8.3.txt", "r", encoding="UTF-8")
    
    txt = f.read()
    
    num = txt.count("snow")
    
    print(num)
    
    f.close()
    I am snow
    i love snow
    nickname is lonelysnow

    输出结果:

    3

Last updated

Was this helpful?