简体   繁体   English

在给定行号的文本文件中打印行

[英]Printing lines in a text file given line numbers

I am having trouble with a while loop statement for the question below.我在处理以下问题的 while 循环语句时遇到问题。 This is for a txt.file.这是一个 txt.file。

'Write a program that allows the user to navigate through the lines of text in any text file. '编写一个程序,允许用户浏览任何文本文件中的文本行。 The program prompts the user for a filename and copies the lines of text from the file into a list.该程序会提示用户输入文件名并将文件中的文本行复制到列表中。 The program then prints the number of lines in the file and prompts the user for a line number.然后程序打印文件中的行数并提示用户输入行号。 Actual line numbers range from 1 to the number of lines in the file.实际行号范围从 1 到文件中的行数。 If the input is 0, the program quits.如果输入为 0,则程序退出。 Otherwise, the program prints the text in that line number.'否则,程序将打印该行号中的文本。

Please see my code.请看我的代码。

enterfile = input("Enter the file name: ")
file = open(enterfile, 'r')
linecount = 0
for line in file:
    linecount = linecount + 1
print("The number of lines in this txt. file is", linecount)
linenum = 0
while True:
num = int(input("Please enter a line number or press 0 to quit: "))
if num >=1 and num <= linecount:
    file = open(enterfile, 'r')
    for lines in file:
        linenum = linenum + 1
        if linenum == num:
            print(lines)
else:
    if num == 0:
        print("Thanks for using the program")
        break

When I run the program, the line number does not print after I enter a line number.运行程序时,输入行号后不打印行号。

Obviously, I am not using the while loop correctly here.显然,我在这里没有正确使用 while 循环。 Can someone please tell me what I am doing wrong here?有人可以告诉我我在这里做错了什么吗? Can I possibly use a def function here?我可以在这里使用 def 函数吗?

Thanks!谢谢!

Seems like you missed the first step of the assignment after the input 好像您错过了输入后分配的第一步

copies the lines of text from the file into a list 将文件中的文本行复制到列表中

With that, you would have 这样,您将拥有

with open(enterfile) as f:
    lines = [line.rstrip() for line in f]
# continue on from here 

Now, forget you even have a file, you can use len(lines) and lines[number-1] to get the total lines and a specific line, respectively 现在,忘记了您甚至有一个文件,您可以分别使用len(lines)lines[number-1]来获取总行数和特定行数

Move line linenum = 0 inside the While True: loop. While True:循环内移动linenum = 0行。

The linenum variable must be reset to 0 ( linenum = 0 ) when the program re-enters the loop. 当程序重新进入循环时,必须将linenum变量重置为0( linenum = 0 )。 Otherwise the linenum variable will always keep being incremented and have a value that is greater than num and will never trigger the if statement to print the line at that number. 否则, linenum变量将始终保持递增状态,并且其值大于num并且永远不会触发if语句以该数字打印行。

Your code with linenum = 0 in the loop: 的循环中linenum = 0 代码

enterfile = input("Enter the file name: ")
file = open(enterfile, 'r')
linecount = 0

for line in file:
    linecount = linecount + 1

print("The number of lines in this txt. file is", linecount)

while True:
    linenum = 0

    num = int(input("Please enter a line number or press 0 to quit: "))
    if num >=1 and num <= linecount:
        file = open(enterfile, 'r')
        for lines in file:
            linenum = linenum + 1
            if linenum == num:
                print(lines)
    else:
        if num == 0:
            print("Thanks for using the program")
            break

Alternative method: 替代方法:

enterfile = input("Enter the file name: ")

with open(enterfile) as f:
    lines = [line.rstrip() for line in f]

print("The number of lines in this txt. file is", len(lines))

while True:
    num = int(input("Please enter a line number or press 0 to quit: "))

    if num > 0 and num < len(lines) + 1:
        print(lines[num - 1]) 
    elif num == 0:
        print('Thanks for using the program.')
        break

use readlines() function return a list that containing the lines then print index value according to user input. 使用readlines()函数返回包含行的列表,然后根据用户输入打印索引值。

file = input("Enter the file name: ")
text_file = open(file, "r")
lines = text_file.readlines()
print (len(lines))
while True:
    linenumber = int(input("Please enter a line number or press 0 to quit:  "))
    if linenumber == 0:
        print("Thanks for using the program")
        break
    elif 1 <= linenumber <= len(lines) :
      print (lines[linenumber- 1])
    else:
        print("Please enter valid line number")
text_file.close()

linenum = 0放入while循环中,即可解决问题。

Polished version:抛光版:

enterfile = input("Enter the input file name: ")
file = open(enterfile, 'r')
linecount = 0
for line in file:
    linecount = linecount + 1
print("The file has",linecount,"lines.")
while True:
    linenum = 0
    num = int(input("Enter a line number [0 to quit]: "))
    if num >=1 and num <= linecount:
        file = open(enterfile, 'r')
        for lines in file:
            linenum = linenum + 1
            if linenum == num:
                print(num,":",lines)
    elif num == 0:
        break
    else:
        if num!= linecount:
            print("ERROR: line number must be less than",linecount)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM