如何将光标移动到 python 中的特定行
How to move the cursor to a specific line in python
我正在从 .txt 文件中读取数据。我需要从某一行开始读取行,所以我不必读取整个文件(使用 .readlines())。因为我知道我应该从哪一行开始阅读,所以我想到了这个(虽然它不起作用):
def create_list(pos):
list_created = []
with open('text_file.txt', 'r') as f:
f.seek(pos) #Here I want to put the cursor at the begining of the line that I need to read from
line = f.readline() #And here I read the first line
while line != '<end>\n':
line = line.rstrip('\n')
list_created.append(line.split(' '))
line = f.readline()
f.close()
return list_created
print(create_list(2)) #Here i need to create a list starting from the 3rd line of my file
我的文本文件看起来像这样:
Something #line in pos= 0
<start> #line in pos= 1
MY FIRST LINE #line in pos= 2
MY SECOND LINE #line in pos= 3
<end>
结果应该类似于:
[['MY', 'FIRST', 'LINE'], ['MY', 'SECOND', 'LINE']]
基本上,我需要从特定行开始我的 readline()。
这个有用吗?如果您不想使用 .readlines()
读取整个文件,您可以通过调用 .readline()
跳过一行。通过这种方式,您可以多次调用 readline()
将光标向下移动,然后 return 下一行。另外,我不建议使用 line != '<end>\n'
除非你绝对确定 <end>
之后会有一个换行符。相反,做类似 not '<end>' in line
:
的事情
def create_list(pos):
list_created = []
with open('text_file.txt', 'r') as f:
for i in range(pos):
f.readline()
line = f.readline() #And here I read the first line
while not '<end>' in line:
line = line.rstrip('\n')
list_created.append(line.split(' '))
line = f.readline()
f.close()
return list_created
print(create_list(2)) #Here i need to create a list starting from the 3rd line of my file
text_file.txt:
Something
<start>
MY FIRST LINE
MY SECOND LINE
<end>
输出:
[['MY', 'FIRST', 'LINE'], ['MY', 'SECOND', 'LINE']]
我正在从 .txt 文件中读取数据。我需要从某一行开始读取行,所以我不必读取整个文件(使用 .readlines())。因为我知道我应该从哪一行开始阅读,所以我想到了这个(虽然它不起作用):
def create_list(pos):
list_created = []
with open('text_file.txt', 'r') as f:
f.seek(pos) #Here I want to put the cursor at the begining of the line that I need to read from
line = f.readline() #And here I read the first line
while line != '<end>\n':
line = line.rstrip('\n')
list_created.append(line.split(' '))
line = f.readline()
f.close()
return list_created
print(create_list(2)) #Here i need to create a list starting from the 3rd line of my file
我的文本文件看起来像这样:
Something #line in pos= 0
<start> #line in pos= 1
MY FIRST LINE #line in pos= 2
MY SECOND LINE #line in pos= 3
<end>
结果应该类似于:
[['MY', 'FIRST', 'LINE'], ['MY', 'SECOND', 'LINE']]
基本上,我需要从特定行开始我的 readline()。
这个有用吗?如果您不想使用 .readlines()
读取整个文件,您可以通过调用 .readline()
跳过一行。通过这种方式,您可以多次调用 readline()
将光标向下移动,然后 return 下一行。另外,我不建议使用 line != '<end>\n'
除非你绝对确定 <end>
之后会有一个换行符。相反,做类似 not '<end>' in line
:
def create_list(pos):
list_created = []
with open('text_file.txt', 'r') as f:
for i in range(pos):
f.readline()
line = f.readline() #And here I read the first line
while not '<end>' in line:
line = line.rstrip('\n')
list_created.append(line.split(' '))
line = f.readline()
f.close()
return list_created
print(create_list(2)) #Here i need to create a list starting from the 3rd line of my file
text_file.txt:
Something
<start>
MY FIRST LINE
MY SECOND LINE
<end>
输出:
[['MY', 'FIRST', 'LINE'], ['MY', 'SECOND', 'LINE']]