2

我不明白“line_count”的值在哪里传递给这个变量。如果您能向我解释一下,我将不胜感激!!输出打印每个连续的行。我了解它如何增加行号(1、2、3、4),但它实际上是如何知道从哪里获取数据以打印每个字符串的,这让我感到困惑。

from sys import argv

script, input_file = argv

def print_all(f):
    print f.read()

def rewind(f):
    f.seek(0)

def print_a_line(line_count, f):
    print line_count, f.readline()

current_file = open(input_file)

print "First let's print the whole file:\n"

print_all(current_file)

print "Now let's rewind, kind of like a tape."

rewind(current_file)

print "Let's print three lines:"

current_line = 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)
4

6 回答 6

2

line_count是函数的参数它通过调用函数时传递参数的调用者获取其值。在这种情况下,参数是current_line全局变量的值。

于 2013-06-22T17:35:23.823 回答
1

倒带后,文件指针回到文件的开头。每次调用都f.readline()将从中读取一行f。在此之后,f文件指针将位于下一行的开头。因此,程序连续读取行。

于 2013-06-22T17:37:44.243 回答
0

如果你看一下这段代码:

current_line = 1
print_a_line(current_line, current_file)

您可以看到 current_line 被传递给 print_a_line 函数,该变量是在函数调用的上方定义的。在函数 print_a_line 中,line_count 是指 current_line 的值。

于 2013-06-22T17:37:51.367 回答
0

您已定义此功能:

def print_a_line(line_count, f):
    print line_count, f.readline()

在这些行中调用它:

current_line = current_line + 1
print_a_line(current_line, current_file)

全局变量current_line的值作为第一个参数传递给函数print_a_line,它绑定到局部变量current_line

于 2013-06-22T18:20:47.697 回答
0

我希望你知道函数如何工作的概念?

如果您不这样做,请在“艰难学习”中再次阅读该部分。

但是,您会知道,在创建这样的函数时:

def print_a_line(line_count, f):
    print line_count, f.readline()

您可以看到有两个参数或也称为参数的东西,被传递给以逗号分隔的函数。在函数中,它们被打印出来。

所以这里使用函数的时候:

current_line = 1 #current_line is 1
    print_a_line(current_line, current_file)

    # the variable current_line is passed into the function, in that first parameter 
    # as a result, 1 is printed

current_line = current_line + 1 # here the variable is assigned its own value + 1
    print_a_line(current_line, current_file)

    # now it will print 2 and that new value in the variable is passed into the function

current_line = current_line + 1
    print_a_line(current_line, current_file)

您需要了解该函数,即使位于文档的顶部,您也可以调用它,或者稍后使用它并传递用于执行该函数执行的操作的值,即打印。

于 2013-06-23T03:21:39.677 回答
0

(答案只是打印出你在哪一行(据说在),但它实际上只是硬编码,所以我认为它有点草率)

I had some issues with this, so looking at this function, it seems that i was under the impression that line_count was the line that you wanted to read, but in fact, all this function actually does is print the line that you hardcoded, and then call f.readline() and it reads each line one at a time as it is called....

i had thought if I had called print_a_line(line_count, f)

for example print_a_line(6, current_file) would give me line 6 of current_file but in reality it goes by how many times you had called f.readline() seems like each time you call this there is a counter within that function that counts how many times it has been called...

so if you called f.readline() 2 times, then it should be on line 3 next..

if you use f.seek(0) and rewind it afterwards, when you call f.readline() it goes back to line 1. and depends on how many f.readline() will read which line...

hope that helps someone else cause I was trying to add to this file, by asking the user which line you want to write and then call this function to print out that line... which didnt do it, so i guess a way I can do it would be to do a for loop to call f.read() whatever I got from the user input, and print out that line.... but you would need to check the number of lines in the file, and make sure the user inputs something less that the total number of lines...

于 2016-01-20T03:59:50.103 回答