简体   繁体   English

如何在Python27中遍历文件而不遇到ValueError并完全用空行遍历文件?

[英]How to iterate through a file in Python27 without running into ValueError and completely iterating through file with empty lines?

I essentially have the same problem as this guy : person also having issues iterating 我基本上有和这个家伙相同的问题:这个人也有迭代的问题

Depending on what I change, I will either run into an IOError, a ValueError (when I use a for each to iterate through each line in the file, and read using readline()), or the program works but it cuts off my data when there's an empty line. 根据我所做的更改,我将遇到IOError,ValueError(当我分别使用IOError遍历文件中的每一行并使用readline()进行读取时),或者程序正常运行,但会切断我的数据空行时。 I've also tried using the for each loop to iterate through the file with .next() instead of readline, but that skips just about every other line in my data set. 我也尝试过使用for each循环通过.next()而不是readline遍历文件,但这几乎跳过了数据集中的所有其他行。 I believe top comment there has the solution to my question, except my text file will have lines that are empty, which ends the while loop too early. 我相信这里的最高注释可以解决我的问题,但我的文本文件中的行将为空,这会导致while循环过早结束。 What is the best way around this? 最好的办法是什么? Is there a better data structure to use, or do I have to somehow parse my file to remove empty lines? 是否有更好的数据结构要使用,还是我必须以某种方式解析文件以删除空行?

Here's a segment of my code, I'm using .rstrip() to get rid of the newline characters at the end of each line: 这是我的代码的一部分,我使用.rstrip()来消除每行末尾的换行符:

f = open(self.path,'r')
    while True:
        line = f.readline().rstrip()
        temp_lines_list.append(line)
        if not line:
            break

Some sample input: 一些样本输入:

text1 : 2380218302
test2 : sad
test3 : moresad (very)
yetanothertest : more datapoints

wowanewsection: incredible

I hope this helps thank you :) 我希望这可以帮助您:)

Have you tried something like this: 您是否尝试过以下方法:

lines_output = []
with open('myFile.txt', 'r') as file: # maybe myFile.txt == self.path??
    for line in file.readlines(): # we use readlines() instead of readline() so we iterate entire file
        stripped_line = line.strip()
        if stripped_line not '':
            lines_output.append(stripped_line) # save info if line is not blank
        else:
            pass # if line is blank just skip it

The readline() method returns a line with a trailing newline character, even on an empty line. readline()方法返回带有尾随换行符的行,即使在空行上也是如此。 You should check if the line is empty before you strip it instead: 您应该先检查该行是否为空,然后再剥离它:

while True:
    line = f.readline()
    if not line:
        break
    temp_lines_list.append(line.rstrip())

However, it is more idiomatic in Python to use the file object as an iterable to iterate through the lines of a file, so that you don't have to manage the iterations on your own. 但是,在Python中,将文件对象作为可迭代对象使用来遍历文件的各行是更惯用的方法,因此您不必自己管理迭代。

for line in f:
    temp_lines_list.append(line.rstrip())

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

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