ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
所属专题:[Python社区](README.md)   ## 问题 **输入:** 一个最多1000行的文本文件。 **输出:** 一个文件,包含原始输入文件的所有偶数行内容。设定行数从1开始计算。 **样例数据:** ``` Bravely bold Sir Robin rode forth from Camelot Yes, brave Sir Robin turned about He was not afraid to die, O brave Sir Robin And gallantly he chickened out He was not at all afraid to be killed in nasty ways Bravely talking to his feet Brave, brave, brave, brave Sir Robin He beat a very brave retreat ``` **样例输出:** ``` Yes, brave Sir Robin turned about And gallantly he chickened out Bravely talking to his feet He beat a very brave retreat ```   ## 背景知识 该问题涉及Python语言的文件读写。详情请查阅ROSALIND网站上[关于该问题的背景说明](http://rosalind.info/problems/ini5/)。   ## 解答 ```python import linecache def evenlines(fin, fout): """从输入文件中提取偶数序的行,输出到另一个文件(行序从1开始)""" with open(fin, 'r') as f1: nrow = len(f1.readlines()) with open(fout, 'w') as f2: for i in range(1, nrow+1): if i%2==0: f2.write(linecache.getline(fin, i)) ## --main-- evenlines("rosalind_ini5.txt", "rosalind_ini5_out.txt") ```