CDays–4 习题六(修改文本)及相关内容解析。

将cdays-4.txt给出的内容中的空格和注释删除,  以行为单位进行排序 ,并将结果输出到result.txt 中。

下面给出cdays-4.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.



看似好难的题。

吓人原因:

1.看似操作复杂

2.竟然是字符串排序

3.竟然是文件中的字符串排序

4.没看过排序算法

5.怎么这么多字

6.文件操作还不熟悉

7.字符串怎么玩好像也不知道


先让我们解决我们知道的内容。

1)打开文件,读取文件,以只读方式打开。

a_file = open ( 'D:\\cdays-4.txt','r' )

2)打开一个新文件,存放结果,以写方式。

result_file = open ( 'D:\\result.txt','w' )

3)将结果写入结果文件

result_file.write(result_str)         #  result_str 是结果序列
result_file.close()

好了。其他的好像都不会。

首先,我们解决读取文件的问题。

在之前的日志中,我们已经提到了文件打开,文件写入,文件关闭,地址:http://www.cnblogs.com/Kaysin/archive/2013/02/09/2909553.html

同样,参考书 《Dive in python 3》

用原书的话说,在Python 中按行读取文件是如此的简单优美:

a_file.readlines (  ):

这货类似于walk ( ) 的用法。

让我们解决文件按行读取的问题:

for line in a_file.readlines (  ):

然后循环体里面应该有的东西.

  1. 判断是否是空行,或者是注释,删除
  2. 删除空白
  3. 将每行存进序列中

解决第一个,空行怎么办,没有字符,长度为0,注释的话,第一个字符为 ‘#’让我们写一个if 判断,如果符合结果,应该跳到下一行,那么用continue 就可以了 。

等等,怎么确定长度和第一个字符,让我们去翻参考书吧。

根据《Python学习手册(第三版)》,使用len( ) 可以确定字符串长度。第一个字符可以用索引实现 :  line[0]

if len(line)==0 or line[0]==('#'):
    continue

怎么删除空白呢,我们继续找。

《Python学习手册(第三版)》P165 有个例子,说的是删除字符串后面的空白,用的是字符串方法,line.rstrip ( )

那么就是说,字符串方法里面有解决方法。让我们在文档中找一下。

原来删除首尾字符串有三个函数,分别是  string.lstrip( ) 删除前面的   string.rstrip( ) 删除后面的  string.strip( ) 删除前后的。

显然我们需要用第三种。

第三个问题,这个序列我们要存入各种字符串,那么,我们应该用list实现。

我们用列表方法。

result_str.append(line)

我们把现在知道的所有语句组合在一起调试一下。

a_file = open ( 'D:\\cdays-4.txt','r' )
result_file = open ( 'D:\\result.txt','w' )
result_str=list();
for line in a_file.readlines (  ):
    if len(line)==0 or line[0]==('#'):
        continue
    line.strip()
    result_str.append(line)

result_file.write(result_str)         #  result_str 是结果序列
result_file.close()

image

发现问题,改为list ,不应该这样输出了。

result_file.write('%s' % '\n'.join(result_str))

观察输出结果,我们发现有一行是空行,调整删除空白与判断字符串长度语句的顺序。

a_file = open ( 'D:\\cdays-4.txt','r' )
result_file = open ( 'D:\\result.txt','w' )
result_str=list();
for line in a_file.readlines (  ):
    line=line.strip()
    if len(line)==0 or line[0]==('#'):
        continue
    
    result_str.append(line)
    
result_file.write('%s' % '\n'.join(result_str))         #  result_str 是结果序列
result_file.close()

好了,我们剩下的工作只有排序了。

把这工作交给Python吧!

result_str.sort(  )

好了,我们把这句话加入到文件写入的前面。 看一下完整的程序吧。

a_file = open ( 'D:\\cdays-4.txt','r' )
result_file = open ( 'D:\\result.txt','w' )
result_str=list();
for line in a_file.readlines (  ):
    line=line.strip()
    if len(line)==0 or line[0]==('#'):
        continue
    result_str.append(line)
result_str.sort(  )    
result_file.write('%s' % '\n'.join(result_str)) #  result_str 是结果序列
result_file.close()

试想一下这个程序如果用C语言,那要写多少行呢,当然上面的程序还不是最简单的。

蛇年快乐,筒子们。

原文地址:https://www.cnblogs.com/Kaysin/p/2909651.html