• 读取文件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. python多线程编程

    Python多线程编程中常用方法: 1.join()方法:如果一个线程或者在函数执行的过程中调用另一个线程,并且希望待其完成操作后才能执行,那么在调用线程的时就可以使用被调线程的join方法join( ...

  2. linux 学习 day1

  3. Appirater -- app中提示用户为app评价的提示框

    Appirater是一段你可以嵌入自己工程中的代码,在用户使用应用一段时间后会自动弹出提示用户进行评分. 使用Appirater方面,你可以简单把源代码嵌入你的app工程中,并把以下代码添加至它的委托 ...

  4. Linux下四款Web服务器压力测试工具(http_load、webbench、ab、siege)介绍

    一.http_load程序非常小,解压后也不到100Khttp_load以并行复用的方式运行,用以测试web服务器的吞吐量与负载.但是它不同于大多数压力测试工具,它可以以一个单一的进程运行,一般不会把 ...

  5. CodeForces 103D 分块处理

    题目链接:http://codeforces.com/problemset/problem/103/D 题意:给定一个长度为n的序列.然后q个询问.每个询问为(a,b),表示从序列第a项开始每b项的加 ...

  6. 首师大附中互测题:LJX的校园:入学典礼【C003】

    [C003]LJX的校园:入学典礼[难度C]—————————————————————————————————————————————————————————————————————————————— ...

  7. [转] JS中简单的继承与多态

    这里讲了一个最最最简单的JS中基于原型链的继承和多态. 先看一下以下这段代码的实现(A是“父类”,B是“子类”): var A = function(){ this.value = 'a'; this ...

  8. [NOIP2011]观光公交 题解

    题目大意: 就省了吧 思路: 应该算是贪心. 不难发现,加速只对所有在使用加速器之后连续的一段下车时不用等人的站点下车的人有用.这非常重要. 先算出不加速时的和,并预处理出每个站点最迟到的人的时间.每 ...

  9. Leetcode Reverse Nodes in k-Group

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  10. 洛谷 P1736 创意吃鱼法 Label:dp || 前缀和

    题目描述 回到家中的猫猫把三桶鱼全部转移到了她那长方形大池子中,然后开始思考:到底要以何种方法吃鱼呢(猫猫就是这么可爱,吃鱼也要想好吃法 ^_*).她发现,把大池子视为01矩阵(0表示对应位置无鱼,1 ...