我想知道如何在.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
我只想打印句子,我不希望它打印“> Sentence#”部分。
有人能帮助我解决这个问题吗?谢谢!
答案 0 :(得分:3)
一个简单的原生Python解决方案,我假设不以using (var pck = new OfficeOpenXml.ExcelPackage())
{
using (FileStream fileStream = new FileStream(dialog.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
pck.Load(fileStream);
}
ExcelWorksheet worksheet = pck.Workbook.Worksheets.First();
DataTable tbl = new DataTable();
//Column Headers
foreach (ExcelRangeBase firstRowCell in worksheet.Cells[9, 2, 9, worksheet.Dimension.End.Column])
{
tbl.Columns.Add(firstRowCell.Text);
}
Int32 startRow = 10;
for (Int32 rowNum = startRow; rowNum <= worksheet.Dimension.End.Row; rowNum++)
{
ExcelRange range = worksheet.Cells[rowNum, 2, rowNum, worksheet.Dimension.End.Column];
DataRow row = tbl.Rows.Add();
foreach (ExcelRangeBase cell in range)
{
//tbl.Rows.Add(cell.Text);
row[cell.Start.Column-2] = cell.Text;
}
}
DTGrid.ItemsSource = tbl.AsDataView();
}
开头的行是'句子'行:
<DataGrid Name="DTGrid" ItemsSource="{Binding Path=DTGrid.ItemsSource}" Grid.Row="1" Grid.RowSpan="2" Grid.Column="0" Grid.ColumnSpan="2" AutoGenerateColumns="true"></DataGrid>
但这实际上是FASTA format(现在很明显这是真的)然后我建议改为使用BioPython:
>
答案 1 :(得分:0)
@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}'")