如何打开文件并找到最长行的长度并打印出来
我到目前为止做了这些,但长度函数没有正常工作。
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()
6 个回答
1
试试这个:
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"
length()
def length():
maxlength = 0
maxlinetext = ""
infile = open("30075165.txt","r")
for line in infile:
linelength = len(line)
if linelength > maxlength:
#If linelength is greater than maxlength value the new value is linelength
maxlength = linelength
maxlinetext = line
print maxlinetext
infile.close()
编辑:添加了 main() 函数。
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
输出结果:
This Should Be Largest Line
作为一个函数:
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")
还有另一种方法,你需要把这个放在一个try/catch里,以处理各种问题(比如空文件等)。
def get_longest_line(filename):
mydict = {}
for line in open(filename, 'r'):
mydict[len(line)] = line
return mydict[sorted(mydict)[-1]]
你还需要决定当有两条“获胜”的行长度相等时该怎么办?选择第一条还是最后一条?前面的函数会返回第一条,后面的函数会返回最后一条。文件内容包括:
Small Line
Small Line
Another Small Line
This Should Be Largest Line
Small Line
更新
你原始帖子中的评论:
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"
让我觉得你打算扫描文件,先找出每行的长度,然后计算ASCII值的总和,再统计单词数量。其实最好是一次性读取文件,然后从中提取你需要的数据。
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
这个函数会返回一个列表,包含文件中每一行的信息,格式为:行内容, 行长度, 行的ASCII总和, 行的单词数量
可以这样使用:
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)
输出结果:
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
你可以像这样把它和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)
32
适用于 Python 2.5 到 2.7.12 版本
print max(open(your_filename, 'r'), key=len)
适用于 Python 3 及更高版本
print(max(open(your_filename, 'r'), key=len))