如何打开文件并找到最长的一行,然后将其打印出来

BFo*_*e01 2 python

这是我到目前为止所做的,但长度函数不起作用.

import string

def main():
    print " This program reads from a file and then prints out the"
    print " line with the longest length the line ,or with the highest sum"
    print " of ASCII values , or the line with the greatest number of words"
    infile = open("30075165.txt","r")
    for line in infile:
        print line
    infile.close()
def length():
    maxlength = 0
    infile = open("30075165.txt","r")
    for line in infile:
        linelength = lengthofline
        if linelength > maxlength:
            #If linelength is greater than maxlength value the new value is linelength
            maxlength = linelength
            linelength = line
    print ,maxlinetext
    infile.close()
Run Code Online (Sandbox Code Playgroud)

Ste*_*eef 28

对于Python 2.5到2.7.12

print max(open(your_filename, 'r'), key=len)
Run Code Online (Sandbox Code Playgroud)

适用于Python 3及更高版本

print(max(open(your_filename, 'r'), key=len))
Run Code Online (Sandbox Code Playgroud)


kjf*_*tch 6

large_line = ''
large_line_len = 0
filename = r"C:\tmp\TestFile.txt"

with open(filename, 'r') as f:
    for line in f:
        if len(line) > large_line_len:
            large_line_len = len(line)
            large_line = line

print large_line
Run Code Online (Sandbox Code Playgroud)

输出:

This Should Be Largest Line
Run Code Online (Sandbox Code Playgroud)

并作为一个功能:

def get_longest_line(filename):
    large_line = ''
    large_line_len = 0

    with open(filename, 'r') as f:
        for line in f:
            if len(line) > large_line_len:
                large_line_len = len(line)
                large_line = line

    return large_line

print get_longest_line(r"C:\tmp\TestFile.txt")
Run Code Online (Sandbox Code Playgroud)

这是另一种方法,你需要将它包装在try/catch中以解决各种问题(空文件等).

def get_longest_line(filename):
    mydict = {}

    for line in open(filename, 'r'):
        mydict[len(line)] = line

    return mydict[sorted(mydict)[-1]]
Run Code Online (Sandbox Code Playgroud)

当你有两条相同长度的"获胜"线时,你还需要决定是否会发生这种情况?先选还是最后?前一个函数将返回第一个,后者将返回最后一个.文件包含

Small Line
Small Line
Another Small Line
This Should Be Largest Line
Small Line
Run Code Online (Sandbox Code Playgroud)

更新

你原帖中的评论:

print " This program reads from a file and then prints out the"
print " line with the longest length the line ,or with the highest sum"
print " of ASCII values , or the line with the greatest number of words"
Run Code Online (Sandbox Code Playgroud)

让我觉得你要扫描文件的行长,然后是ascii sum,然后是单词的数量.最好一次读取文件然后从结果中提取所需的数据.

def get_file_data(filename):
    def ascii_sum(line):
        return sum([ord(x) for x in line])
    def word_count(line):
        return len(line.split(None))

    filedata = [(line, len(line), ascii_sum(line), word_count(line)) 
                for line in open(filename, 'r')]

    return filedata
Run Code Online (Sandbox Code Playgroud)

此函数将以以下格式返回文件的每一行的列表: line, line_length, line_ascii_sum, line_word_count

这可以这样使用:

afile = r"C:\Tmp\TestFile.txt"

for line, line_len, ascii_sum, word_count in get_file_data(afile):
    print 'Line: %s, Len: %d, Sum: %d, WordCount: %d' % (
        line.strip(), line_len, ascii_sum, word_count)
Run Code Online (Sandbox Code Playgroud)

输出:

Line: Small Line, Len: 11, Sum: 939, WordCount: 2
Line: Small Line, Len: 11, Sum: 939, WordCount: 2
Line: Another Small Line, Len: 19, Sum: 1692, WordCount: 3
Line: This Should Be Largest Line, Len: 28, Sum: 2450, WordCount: 5
Line: Small Line, Len: 11, Sum: 939, WordCount: 2
Run Code Online (Sandbox Code Playgroud)

您可以将其与Steef的解决方案混合使用,如下所示:

>>> afile = r"C:\Tmp\TestFile.txt"
>>> file_data = get_file_data(afile)
>>> max(file_data, key=lambda line: line[1]) # Longest Line
('This Should Be Largest Line\n', 28, 2450, 5)
>>> max(file_data, key=lambda line: line[2]) # Largest ASCII sum
('This Should Be Largest Line\n', 28, 2450, 5)
>>> max(file_data, key=lambda line: line[3]) # Most Words
('This Should Be Largest Line\n', 28, 2450, 5)
Run Code Online (Sandbox Code Playgroud)