gpt4 book ai didi

python - 如何打开文件并找到最长的一行然后打印出来

转载 作者:行者123 更新时间:2023-11-28 19:30:32 28 4
gpt4 key购买 nike

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

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()

最佳答案

对于 Python 2.5 到 2.7.12

print max(open(your_filename, 'r'), key=len)

对于 Python 3 及更高版本

print(max(open(your_filename, 'r'), key=len))

关于python - 如何打开文件并找到最长的一行然后打印出来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1292630/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com