python - readline() 在源文件中跳过行

标签 python readline accumulator

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

当我使用计数累加器运行 for 循环时,它会跳过行。

它跳过第一行,从第二行开始,打印第四行、第六行等等。

我缺少什么?

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()”?我怀疑“for line in data_file:”也是一个 readline 操作。

关于python - readline() 在源文件中跳过行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6618882/

相关文章:

Python 解码对我有效,但对其他人无效

python - 转换为 float 而不舍入小数位

Python 使用 in 和 not in 删除字符串中的重复项

python - 困惑为什么我不能将字符串添加在一起

assembly - 累加器寄存器 8086 微处理器系列

python - numpy数组与任意维度数组的乘法

python - list.append 或 list +=?

java - Java中斐波那契序列中的BufferedReader错误

python - 使用 Paramiko 执行交互式工具的命令并读取其输出

javascript - 覆盖多个先前的终端输出