• 读取文件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 == "\r\n" or line == "\n" or line.startswith("#"):
continue newlines.append(line) #按照行进行排序
newlines.sort()
wFile.write("%s" % "".join(newlines)) rFile.closed
wFile.closed

可爱的Python_课后习题_CDay−4 可用的首个Python 脚本的更多相关文章

  1. 可爱的Python_课后习题_CDay−2 完成核心功能

    1. 在前文的grep 实现例子中,没有考虑子目录的处理方式,因为如果直接open 目录进行读grep 是古老实用且高效的模式文本匹配工具,在所有的Unix/Linux 系统中都会默认安装,它最常做的 ...

  2. 可爱的Python_课后习题_CDay−3 通过函式进行功能化

    1. 读取某一简单索引文件cdays−3-test.txt,其每行格式为:文档序号关键词,现须根据这些信息将它转化为倒排索引,即统计关键词在哪些文档中,格式如下:包含该关键词的文档数关键词 => ...

  3. 可爱的Python_课后习题_CDay−5 Python 初体验和原始需求

    计算今年是否是闰年.判断闰年条件,满足年份模400 为0,或者模4 为0 但模100不为0. def is_learp_year(year): """判断年份是否为润年& ...

  4. 可爱的Python_课后习题_CDay0 时刻准备着!发布

    请根据软件发布的流程和软件开发的编码规范,将读者在前面章节所写的程序修改并发 布出去.另外,可以查找下除了 epydoc 外还有哪些较好的 py 文档生成器? pydoc是Python自带的模块,主要 ...

  5. CentOS7入门到精通实战课程课后习题

    Linux自动化运维系列①: CentOS7入门到精通实战--->传送门 http://edu.51cto.com/course/13055.html 01.系统入门课后习题 1.口述一个命令执 ...

  6. 《python核心编》程课后习题——第三章

    核心编程课后习题——第三章 3-1 由于Python是动态的,解释性的语言,对象的类型和内存都是运行时确定的,所以无需再使用之前对变量名和变量类型进行申明 3-2原因同上,Python的类型检查是在运 ...

  7. web实验指导书和课后习题参考答案

    实验指导书 :http://course.baidu.com/view/daf55bd026fff705cc170add.html 课后习题参考答案:http://wenku.baidu.com/li ...

  8. 《Python核心编程》 第六章 序列 - 课后习题

    课后习题 6–1.字符串.string 模块中是否有一种字符串方法或者函数可以帮我鉴定一下一个字符串是否是另一个大字符串的一部分? 答:成员关系操作符(in.not in) import string ...

  9. 《Python核心编程》 第五章 数字 - 课后习题

    课后习题  5-1 整形. 讲讲 Python 普通整型和长整型的区别. 答:普通整型是绝大多数现代系统都能识别的. Python的长整型类型能表达的数值仅仅与你机器支持的(虚拟)内存大小有关. 5- ...

随机推荐

  1. [BI项目记]-DB脚本同步

    BI项目中会有很多不同种类的项目,其中比较比较大的一部分就是对DB脚本的处理.然而DB的脚本毕竟无法在项目中进行维护,所以这里介绍如何对DB的脚本进行版本上的维护. 数据库脚本本身没有项目模板支持,很 ...

  2. 解决VS2015启动界面卡在白屏的处理方法

    最后,通过还原初始配置的方法:使用命令devenv /ResetSettings

  3. 决策树的python实现

    决策树 算法优缺点: 优点:计算复杂度不高,输出结果易于理解,对中间值缺失不敏感,可以处理不相关的特征数据 缺点:可能会产生过度匹配的问题 适用数据类型:数值型和标称型 算法思想: 1.决策树构造的整 ...

  4. Android 笔记 day2 拨号器

  5. json-lib——JsonConfig详细使用说明

    在使用json-lib包中JSONObject.fromObject(bean,cfg)时,可能出现以下几种情况: 1.(防止自包含)转换的对象包含自身对象,或者对象A下面挂了对象B,对象B下面又挂了 ...

  6. Spring--通过注解来配置bean【转】

    Spring通过注解配置bean 基于注解配置bean 基于注解来配置bean的属性在classpath中扫描组件 组件扫描(component scanning):Spring能够从classpat ...

  7. 循环遍历DataTable绑定到Table

    VoteList2.cs: using System; using System.Collections.Generic; using System.Linq; using System.Web; u ...

  8. android解析json

    android2.3提供的json解析类 android的json解析部分都在包org.json下,主要有以下几个类: JSONObject:可以看作是一个json对象 JSONStringer:js ...

  9. Codeforces525E Anya and Cubes(双向搜索)

    题目 Source http://codeforces.com/contest/525/problem/E Description Anya loves to fold and stick. Toda ...

  10. css before,after伪元素妙用

    我们知道,css伪元素包括after,before,first-letter等,通过合理的利用伪元素,我们可以让我们的结构更简洁. 通常写法如p::after{content:' '},其中conte ...