m0_52235516 2021-10-29 10:14 采纳率: 76.5%
浏览 179
已结题

.编写程序,实现以下功能: 1) 打开一个文本文件, 将一篇英文文章的内容写入其中并保存;

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

  • 写回答

2条回答 默认 最新

  • Fioman_Hammer 2021-10-29 10:46
    关注
    
    
    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日

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真