我想帮助弄清楚如何在.txt文件中只打印出给定数量的行。
我创建了一个带有2个输入参数的函数文件(x,y),第一个'x'是文件,第二个'y'决定了它要打印多少行。
示例:假设文件名为x.txt,文件内的内容为:
>Sentence 1
I like playing games
>Sentence 2
I like jumping around
>Sentence 3
I like dancing
>Sentence 4
I like swimming
>Sentence 5
I like riding my bike
我想用这些内容做的就是让它读取然后在我调用文件(“x.txt”,3)时打印出文件中的句子,所以它只打印前3行,就像这样样本输出:
'I like playing games'
'I like jumping around'
'I like dancing'
这是我到目前为止所做的:
def file(x, y):
file = open(x, 'r')
g = list(range(y))
h = [a for i, a in enumerate(file) if i in g]
return " ' ".join(h)
我无法弄清楚如何让程序打印用户输入的行数,但到目前为止我运行程序时这是我得到的:
>Sentence 1
' I like playing games
' >Sentence 2
我只想打印句子,我不希望它打印“>句子#”部分。
有人能帮我解决这个问题吗?谢谢!
一个简单的原生Python解决方案,我假设不以>
开头的行是'句子'行:
from itertools import islice
def extract_lines(in_file, num):
with open(in_file) as in_f:
gen = (line for line in in_f if not line.startswith('>'))
return '\n'.join(islice(gen, num))
但这实际上是FASTA format(现在很明显这是真的)然后我建议使用BioPython代替:
from Bio import SeqIO
from itertools import islice
def extract_lines(in_file, num):
with open(in_file) as in_f:
gen = (record.seq for record in SeqIO.parse(in_f, 'fasta'))
return list(islice(gen, num))
@Chris_Rands给出的答案很好,但是由于你在评论中要求没有导入的解决方案,这里有一种可能性:
def extract_lines(in_file, num):
"""This function generates the first *num* non-header lines
from fasta-formatted file *in_file*."""
nb_outputted_lines = 0
with open(in_file, "r") as fasta:
for line in fasta:
if nb_outputted_lines >= num:
break # This interrupts the for loop
if line[0] != ">":
yield line.strip() # strip the trailing '\n'
nb_outputted_lines += 1
要使用它:
for line in extract_lines("x.txt", 3):
print(line)
# If you want the quotes:
#print("'%s'" % line)
# Or (python 3.6+):
#print(f"'{line}'")