Python 3.2-readline()跳过源文件中的行 [英] Python 3.2 - readline() is skipping lines in source file

查看:461
本文介绍了Python 3.2-readline()跳过源文件中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多行创建的.txt文件。

I have a .txt file that I created with multiple lines.

当我运行带有计数累加器的for循环时,它会跳过行。

when I run a for loop, with a count accumulator, it skips lines.

跳过第一行,从第二行开始,打印第四行,第六行,等等...

It skips the top line, and starts with the second, prints the fourth, the sixth, etc...

我想念的是什么?

** for your reading pleasure**
def main():
    # Open file line_numbers.txt
    data_file = open('line_numbers.txt', 'r')

    # initialize accumulatior
    count = 1


    # Read all lines in data_file
    for line in data_file:
        # Get the data from the file
        line = data_file.readline()

        # Display data retrieved
        print(count, ": ", line)

        # add to count sequence
        count += 1


推荐答案

尝试完全删除 line = data_file.readline()吗?我怀疑 data_file中的for行也是一个readline操作。

Try removing the "line=data_file.readline()" altogether? I suspect the "for line in data_file:" is also a readline operation.

这篇关于Python 3.2-readline()跳过源文件中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆