简体   繁体   English

逐行读取文件,而不是将文件读取到内存

[英]Read a file line-by-line instead of read file into memory

I´m having some trouble with reading a file line-by-line, instead of reading the the file into memory. 我在逐行读取文件时遇到了麻烦,而不是将文件读取到内存中。 As of right now, I'm reading the file into memory, and it works perfect. 截至目前,我正在将文件读入内存,并且可以正常运行。 However, If i try to read the file line-by-line, I only get zero when I type 'print(B)'. 但是,如果我尝试逐行读取文件,则在键入“ print(B)”时只能得到零。 My question is, does anyone have a good command for reading the file line-by-line in python? 我的问题是,有人在python中逐行读取文件吗? My code looks like this: 我的代码如下所示:

def read(filename):

    with open(filename, 'r') as f: #open the file

        for line in f:

            A = sum(float(line) for line in f)

    with open(filename, 'r') as f:

            B = sum(float(line)**2 for line in f)

            print(B)

read('file.txt')

Here is a way to do it with only one pass over the file. 这是一种仅通过文件传递一次的方法。 You have to abandon the nice built-in sum and do it yourself: 您必须放弃内置的sum ,自己动手做:

def read(filename):
    A, B = 0, 0
    with open(filename) as f:
        for line in f:
            x = float(line)
            A += x
            B += x**2
    print(A)
    print(B)

Also note that you are actually iterating in a weird way over the lines of the file, since you have an outer loop for line in f and an inner loop in the sum that also runs over for line in f . 另外请注意,你实际上是在通过文件的行奇怪的是迭代,因为你有一个外循环for line in f ,并在一个内部循环sum还运行在for line in f Since f is an iterator, this means that the outer loop will only get to the first line, the inner loop will consume all other lines and sum them and then the outer loop has nothing else to process and quits. 由于f是一个迭代器,这意味着外循环将仅到达第一行,内循环将消耗所有其他行并将它们相加,然后外循环没有其他要处理并退出。 You should be able to see this by noting that the print(B) statement is only executed once. 通过注意到print(B)语句仅执行一次,您应该能够看到这一点。

To return to the beginning of the file, use seek : 要返回文件的开头,请使用seek

def read(filename):

    with open(filename, 'r') as f: #open the file

        A = sum(float(line) for line in f)
        f.seek(0)
        B = sum(float(line)**2 for line in f)

            print(B)

Is this right for you? 这适合您吗?

with open(filename, 'r') as f:
    data = f.readlines()

A = sum(float(line) for line in data)
B = sum(float(line)**2 for line in data)

Original problem is that you've hit the end of the file and need to go back to the start to iterate over it again. 最初的问题是,您已经打到文件的末尾,需要重新开始重新遍历该文件。 You can do this in a single iteration through the file, split into two and then sum. 您可以在整个文件中进行一次迭代,然后将其拆分为两个,然后求和。

with open(filename) as f:
    A, B = map(sum, zip(*((x, x**2) for x in map(float, f))))

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

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