TickingClock的个人博客分享 http://blog.sciencenet.cn/u/TickingClock

博文

Rosalind 5 - Working with Files

已有 3733 次阅读 2017-10-21 08:08 |个人分类:Python Learning|系统分类:科研笔记

Python Village - INI5: Working with Files


Reading and Writing


Python中读写文件是用的open()函数,共有三种模式:r(read mode),w(write mode)和a(append mode)。


比如,我们新建一个txt文档,命名为input.txt,然后粘贴进


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


在Python中输入:


>>> f = open('input.txt', 'r')

>>>


这是,你就告诉Python去读取一个叫input.txt的文档,模式是read mode,读取后存放于f中(注意目录要一致,否则读取不了)。


要从f中获取数据有以下几种方法:


>>> f = open('input.txt', 'r')
>>> f.read(10)
'Bravely bo'

>>>


从f中获取10个字节的数据


>>> f = open('input.txt', 'r')
>>> f.readline()
'Bravely bold Sir Robin rode forth from Camelot\n'

>>>


从f中获取第一行的数据


>>> f = open('input.txt', 'r')
>>> f.readline().strip()
'Bravely bold Sir Robin rode forth from Camelot'

>>>


从f中获取第一行的数据,不带\n(换行符)


>>> f = open('input.txt', 'r')
>>> f.readlines()
['Bravely bold Sir Robin rode forth from Camelot\n', 'Yes, brave Sir Robin turned about\n', 'He was not afraid to die, O brave Sir Robin\n', 'And gallantly he chickened out\n', 'He was not at all afraid to be killed in nasty ways\n', 'Bravely talking to his feet\n', 'Brave, brave, brave, brave Sir Robin\n', 'He beat a very brave retreat']

>>>


从f中获取所有行的数据


>>> f = open('input.txt', 'r')
>>> f.readlines()[2]
'He was not afraid to die, O brave Sir Robin\n'

>>>


从f中获取第三行的数据(注意Python是从0开始计数的)


我们还可以利用loop去读取数据,比如:


>>> f = open('input.txt', 'r')
>>> for line in f:
...        print line
...
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

>>>


另外,对于文档内的内容不是用的换行符进行分隔的,我们可以利用line.split()来进行分隔,比如:


>>> 'Beautiful is better than ugly.\n'.split()
['Beautiful', 'is', 'better', 'than', 'ugly.']

>>>


利用空格进行分隔


>>> 'Explicit, is better, than implicit.'.split(",")
['Explicit', ' is better', ' than implicit.']

>>>


利用逗号进行分隔


>>> 'Simple is\nbetter than\ncomplex.\n'.splitlines()
['Simple is', 'better than', 'complex.']

>>>


利用换行符进行分隔


Python中写文件时也是先用open()函数:


>>> f = open('output.txt', 'w')

>>>


python就会在该目录下创建一个空的txt文档,命名为output.txt。然后,用.write()函数:


>>> f.write('Any data you want to write into file')
>>> f.close()

>>>


output.txt文档中就会写入:


Any data you want to write into file


如果是想写入lists或者integers的话,则需要先用str()函数将其转变为strings:


>>> f = open('output.txt', 'w')
>>> inscription = ['Rosalind Elsie Franklin ', 1920, 1958]
>>> s = str(inscription)
>>> f.write(s)
>>> f.close()

>>>


这样文档中写入的内容就是


['Rosalind Elsie Franklin ', 1920, 1958]


同样,我们也可以借助于loop来写入:


>>> f = open('output.txt', 'w')
>>> for i in inscription:
...        f.write(str(i) + '\n')
...
>>> f.close()

>>>


文档中写入的内容为:


Rosalind Elsie Franklin
1920

1958


Problem


Given: A file containing at most 1000 lines.


Return: A file containing all the even-numbered lines from the original file. Assume 1-based numbering of lines.


Sample Dataset


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


Sample Output


Yes, brave Sir Robin turned about
And gallantly he chickened out
Bravely talking to his feet

He beat a very brave retreat


Solution


先新建一个txt文档,命名为input.txt,然后把


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


粘贴进去,之后:


>>> f = open("input.txt", "r")
>>> d = f.readlines()
>>> for i in range(1, len(d)|1, 2):
...        print d[i].strip()
...
Yes, brave Sir Robin turned about
And gallantly he chickened out
Bravely talking to his feet
He beat a very brave retreat

>>>


另外,看到有网友Martin Schweitzer写的答案更加简洁:


>>> print ''.join(open('input.txt').readlines()[1::2])
Yes, brave Sir Robin turned about
And gallantly he chickened out
Bravely talking to his feet
He beat a very brave retreat

>>>


其中[a::b]的意思是告诉Python从第a+1个item开始(Python中第一个编号为0)每隔b-1个item取一次。


Over


Rosalind is a platform for learning bioinformatics and programming through problem solving. Take a tour to get the hang of how Rosalind works.


P.S. 欢迎关注微信公众号:微信号Plant_Frontiers




https://blog.sciencenet.cn/blog-3158122-1081798.html

上一篇:Plant Cell:拮抗转录因子复合物调控水稻成花转变
下一篇:Plant Physiology:类黄酮调控ABA诱导的ROS以控制气孔的开闭
收藏 IP: 221.181.145.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-9-27 08:17

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部