从python文件中逐行读取

这个问题在这里已经有了答案:

  • 在Python中,如何逐行读取文件到列表中? 35个答案

  • 您需要遍历文件而不是行:

    #! /usr/bin/python
    file = open('/home/results/err.txt')
    for line in file:
        print line
    

    file.readline()只读取第一行。 当你遍历它时,你正在迭代该行中的字符。


    file.readline()已经读取一行。 迭代该行会为您提供单个字符。

    相反,使用:

    for line in file:
        …
    

    尝试这个 :

    #! /usr/bin/python
    file = open('/home/results/err.txt')
    for line in file.readlines():
        print line
    
    链接地址: http://www.djcxy.com/p/42355.html

    上一篇: Reading line by line from a file in python

    下一篇: How can I store from a file to array in Python?