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

时间:2018-11-08 04:44:48

标签: python python-3.x function while-loop user-defined-functions

我在处理以下问题的while循环语句时遇到麻烦。这是一个txt.file。

  

'编写一个程序,该程序允许用户浏览任何文本文件中的文本行。该程序提示用户输入文件名,并将文本行从文件复制到列表中。然后,程序将打印文件中的行数,并提示用户输入行号。实际的行号范围是1到文件中的行数。如果输入为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)
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

运行程序时,输入行号后,行号不会打印。

很显然,我在这里没有正确使用while循环。有人可以告诉我我在做什么错吗?我可以在这里使用def函数吗?

谢谢!

5 个答案:

答案 0 :(得分:2)

好像您错过了输入后分配的第一步

  

将文件中的文本行复制到列表中

有了它,您将拥有

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

现在,忘记了您甚至有一个文件,可以分别使用len(lines)lines[number-1]来获取总行数和特定行数

答案 1 :(得分:1)

<div class="row"> <div class="sg_formslist"> <div class="col-sm-12 col-md-12"> <div id="Selection-1-container" > <form class="form-horizontal" id="form1"> <div class="col-sm-12 "> <div class="col-sm-2"> </div> <div class="col-sm-6"> <div class="form-group"> <div class="col-sm-12"> <label class="control-label col-sm-6">Surname<span class="impstar">*</span></label> <div class="col-sm-6"> <input type="text" class="form-control" name="customer_name" Placeholder="Customer Name" required> </div> </div> </div> <div class="form-group"> <div class="col-sm-12"> <label class="control-label col-sm-6">Mobile Number 1<span class="impstar">*</span></label> <div class="col-sm-6"> <input type="tel" name="mob_num1" class="form-control" size="10" maxlength="10" Placeholder="+91 - 83445839284" required> </div> </div> </div> <div class="form-group"> <div class="col-sm-12"> <label class="control-label col-sm-6">Mobile Number 2<span class="impstar">*</span></label> <div class="col-sm-6"> <input type="tel" name="mob_num1" class="form-control" size="10" maxlength="10" Placeholder="+91 - 83445839284 "> </div> </div> </div> <div class="form-group "> <div class="col-sm-12"> <label class="control-label col-sm-6">Gender<span class="impstar">*</span></label> <div class="col-sm-6"> <div class="radio" required> <label > <input type="radio" name="Selection" id="Selection-1" value="female" checked> Female </label> <label > <input type="radio" name="Selection" id="Selection-1" value="male" > Male </label> </div> </div> </div> </div> <div class="form-group"> <div class="col-sm-12"> <label class="control-label col-sm-6">Description <span class="impstar">*</span></label> <div class="col-sm-6"> <textarea class="form-control" rows="3" name="description" placeholder="Description For Discount"></textarea> </div> </div> </div> <div class="form-group text-right"> <i class="fa fa-paper-plane plnay" ></i> <input type="submit" class="btn btn-info" value="Submit "> </div> </div> <div class="col-sm-3"> </div> </div> </div> </form> </div> </div> </div> </div> 循环内移动linenum = 0行。

程序重新进入循环时,必须将While True:变量重置为0(linenum)。否则,linenum = 0变量将始终保持递增状态,并且其值大于linenum,并且永远不会触发if语句以该数字打印行。

您的代码,其中包含num

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

答案 2 :(得分:1)

使用readlines()函数返回一个包含行的列表,然后根据用户输入打印索引值。

<div class="container">
    <div class=row style="display: block;">
        <div class="col-12 col-md-5 order-md-2" style="float:right;">
            <h3>Top Bar</h3>
        </div>

        <div class="col-12 col-md-7 order-md-1" style="float:left;">
            <h3>Content</h3>
            <p>A bit of Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent ac turpis ac massa mollis posuere.</p>
            <p>Duis urna purus, sagittis eget fermentum in, aliquam sed est.</p> 
            <p>Praesent imperdiet a nisi at aliquet. Curabitur velit mi, vestibulum sed molestie non, egestas sit amet elit.</p>
            <p>Donec commodo tincidunt ligula sed pharetra. Etiam efficitur blandit laoreet. Aliquam eu pellentesque dui, eu accumsan dolor. Vestibulum congue facilisis porta. Praesent venenatis, risus eu mollis varius, erat est ornare felis, in rhoncus arcu metus eget ante.</p>
        </div>

        <div class="col-12 col-md-5 order-md-2"  style="float:right;" >
            <h3>Bottom Bar</h3>
        </div>
    </div>
</div>

答案 3 :(得分:0)

dfMRD1放入while循环中,问题就解决了。

答案 4 :(得分:0)

抛光版:

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)