zoukankan      html  css  js  c++  java
  • 可爱的Python_课后习题_CDay−4 可用的首个Python 脚本

    • 读取文件cdays−4-test.txt 内容,去除空行和注释行后,以行为单位进行排序,并将结果输出为cdays−4-result.txt。

      cdays−4-test.txt的内容

    #some words
    Sometimes in life,
    You find a special friend;
    Someone who changes your life just by being part of it.
    Someone who makes you laugh until you can't stop;
    Someone who makes you believe that there really is good in the world.
    Someone who convinces you that there really is an unlocked door just waiting 
    for you to open it.
    
    
    This is Forever Friendship.
    when you're down,
    and the world seems dark and empty,
    Your forever friend lifts you up in spirits and makes that dark and empty
    world
    suddenly seem bright and full.
    Your forever friend gets you through the hard times,the sad times,and the
    confused times.
    If you turn and walk away,
    Your forever friend follows,
    If you lose you way,
    Your forever friend guides you and cheers you on.
    Your forever friend holds your hand and tells you that everything is going
    to be okay.

    cdays−4-result.txt的内容

    If you lose you way,
    If you turn and walk away,
    Someone who changes your life just by being part of it.
    Someone who convinces you that there really is an unlocked door just waiting 
    Someone who makes you believe that there really is good in the world.
    Someone who makes you laugh until you can't stop;
    Sometimes in life,
    This is Forever Friendship.
    You find a special friend;
    Your forever friend follows,
    Your forever friend gets you through the hard times,the sad times,and the
    Your forever friend guides you and cheers you on.
    Your forever friend holds your hand and tells you that everything is going
    Your forever friend lifts you up in spirits and makes that dark and empty
    and the world seems dark and empty,
    confused times.
    for you to open it.
    suddenly seem bright and full.
    to be okay.when you're down,
    world


    python程序

    #-*- coding:utf-8 -*-
    import os
    
    if __name__ == "__main__":
    
        rFile = open(u"E:/cdays-4-test.txt", "r")
        wFile = open(u"E:/cdays-4-result.txt", "w")
    
        lines = rFile.readlines()
        newlines = list()
        for line in lines:
            #去掉空行和注释
            if line == "
    " or line == "
    " or line.startswith("#"):
                continue
    
            newlines.append(line)
    
        #按照行进行排序
        newlines.sort()
        wFile.write("%s" % "".join(newlines))
    
        rFile.closed
        wFile.closed
  • 相关阅读:
    k8s集群中遇到etcd集群故障的排查思路
    keepalived安装
    python读取文件特定的行数
    Pycharm 退回跳转之前光标页面位置
    python中yield的用法详解——最简单,最清晰的解释
    np.random.permutation()解析
    处理文本分类数据集——THUCNews数据
    [深度学习] PyTorch 实现双向LSTM 情感分析
    lstm模型与情感分析实例
    跑Bert还得用tensorflow-1.11.0版本,否则报错
  • 原文地址:https://www.cnblogs.com/zhuhaiying/p/6181868.html
Copyright © 2011-2022 走看看