-1

Here is the code I'm working with

def ascii_sum():
    x = 0
    infile = open("30075165.txt","r")
    for line in infile:
        return sum([ord(x) for x in line])
    infile.close()

This code only prints out the first ASCII value in the file not the max ASCII value

lavinio
  • 23,931
  • 5
  • 55
  • 71
BForce01
  • 69
  • 1
  • 2
  • 7
  • 1
    you can't expand on your previous question? – SilentGhost Aug 18 '09 at 12:04
  • 2
    I already have a solution to this in the update of your previous question: http://stackoverflow.com/questions/1292630/how-to-open-a-file-and-find-the-longest-length-of-a-line-and-then-print-it-out/1292676#1292676 I bet this also answeres your NEXT question also. – kjfletch Aug 18 '09 at 12:09

2 Answers2

1
max(open(fname), key=lambda line: sum(ord(i) for i in line))
SilentGhost
  • 307,395
  • 66
  • 306
  • 293
  • +1 and if he wants the line number he can do: max(enumerate(open(fname)), key=lambda (i, line): sum(ord(i) for i in line)) – Nadia Alramli Aug 18 '09 at 12:11
1

This is a snippet from an answer to one to of your previous questions

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': line, 
                 'line_len': len(line), 
                 'ascii_sum': ascii_sum(line), 
                 'word_count': word_count(line)}
                for line in open(filename, 'r')]

    return filedata

afile = r"C:\Tmp\TestFile.txt"
file_data = get_file_data(afile)

print max(file_data, key=lambda line: line['line_len']) # Longest Line
print max(file_data, key=lambda line: line['ascii_sum']) # Largest ASCII sum
print max(file_data, key=lambda line: line['word_count']) # Most Words
Community
  • 1
  • 1
kjfletch
  • 5,394
  • 3
  • 32
  • 38