weixin_51033362 2021-10-29 10:14 采纳率: 100%
浏览 42
已结题

编写程序,实现以下功能。

1)
打开一个文本文件,
将一篇英文文章的内容写入其中并保存;
2)
读取该文件中的内容,
统计文中单词的数目以及每个单词出
现的次数,并进行输出显示;
3)
对文中的内容做修改,
即是把其中的大写字母转换为小写字
母,小写字母转换为大写字母;
4)关闭文件。

  • 写回答

2条回答 默认 最新

  • Fioman_Hammer 2021-10-29 10:44
    关注
    
    # @Time : 2021/10/29 10:20
    # @Author : Fioman 
    # @Phone : 13149920693
    import string
    
    article = """
        There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! 
    Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do 
    all the things you want to do.
        May you have enough happiness to make you sweet,enough trials to make you strong,
    enough sorrow to keep you human,enough hope to make you happy? Always put yourself in others’shoes.
        If you feel that it hurts you,it probably hurts the other person, too.
    The happiest of people don’t necessarily have the best of everything;they just make the most 
    of everything that comes along their way.Happiness lies for those who cry,those who hurt, 
    those who have searched,and those who have tried,for only they can appreciate the importance of people
    who have touched their lives.Love begins with a smile,grows with a kiss and ends with a tear.
    The brightest future will always be based on a forgotten past, 
    you can’t go on well in lifeuntil you let go of your past failures and heartaches.
    """
    # 1) 打开一个文本文件
    file = open("article.txt","w+")
    file.write(article) # 写入
    file.close()
    file = open("article.txt","r")
    data = file.read()
    
    # 2) 统计次数
    # 先将标点符号和换行符全部替换成空格
    for c in string.punctuation:
        data = data.replace(c," ") # 将标点符号转换成空格
    # 将换行符替换为空格
    data = data.replace("\n"," ")
    
    words = data.strip().split(" ")
    print(words)
    wordDict = {}
    for word in words:
        if word == "" or word == " ":
            continue
        if word not in wordDict:
            wordDict[word] = 1
        else:
            wordDict[word] = wordDict[word] + 1
    
    # 输出显示
    for word,count in wordDict.items():
        print("单词: {} 出现次数: {}".format(word,count))
    
    # 3)修改
    newStr = ""
    for s in data:
        if s.isupper():
            s = s.lower()
        elif s.islower():
            s = s.upper()
    
        newStr += s
    
    # 4) 修改之后保存
    file2 = open("new_article.txt","w")
    file2.write(newStr)
    
    # 5)关闭
    file.close()
    file2.close()
    
    
    

    如果觉得答案对你有帮助,请点击下采纳,谢谢~

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月6日
  • 已采纳回答 10月29日
  • 创建了问题 10月29日

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向