'Python - Get the largest age from a list txt.file

any idea how should I get the largest age from the text file and print it?

The text file:

Name, Address, Age,Hobby
Abu, “18, Jalan Satu, Penang”, 18, “Badminton, Swimming”
Choo,  “Vista Gambier, 10-3A-88, Changkat Bukit Gambier Dua, 11700, Penang”, 17, Dancing
Mutu, Kolej Abdul Rahman, 20, “Shopping, Investing, Youtube-ing”

This is my coding:

with open("iv.txt",encoding="utf8") as file:
data = file.read()
splitdata = data.split('\n')

I am not getting what I want from this.



Solution 1:[1]

This works! I hope it helps. Let me know if there are any questions. This approach essentially assumes that values associated with Hobby do not have numbers in them.

import csv

max_age = 0
with open("iv.txt", newline = '', encoding = "utf8") as f:
    # spamreader returns reader object used to iterate over lines of f
    # delimiter=',' is the default but I like to be explicit
    spamreader = csv.reader(f, delimiter = ',')
    # skip first row
    next(spamreader)
    # each row read from file is returned as a list of strings
    for row in spamreader:
        # reversed() returns reverse iterator (start from end of list of str)
        for i in reversed(row):
            try:
                i = int(i)
                break
            # ValueError raised when string i is not an int
            except ValueError:
                pass
        print(i)
        if i > max_age:
            max_age = i

print(f"\nMax age from file: {max_age}")

Output:

18
17
20

Max age from file: 20

spamreader from the csv module of Python's Standard Library returns a reader object used to iterate over lines of f. Each row (i.e. line) read from the file f is returned as a list of strings.

The delimiter (in our case, ',', which is also the default) determines how a raw line from the file is broken up into mutually exclusive but exhaustive parts -- these parts become the elements of the list that is associated with a given line.

Given a raw line, the string associated with the start of the line to the first comma is an element, then the string associated with any part of the line that is enclosed by two commas is also an element, and finally the string associated with the last comma to the end of the line is also an element.

For each line/list of the file, we start iterating from the end of the list, using the reversed built-in function, because we know that age is the second-to-last category. We assume that the hobby category does not have numbers in them such that the number would appear as an element of the list for the raw line. For example, for the line associated with Abu, if instead of "Badminton, Swimming" we had "Badminton, 30, Swimming", then the code would not have the desired effect as 30 would be treated as Abu's age.

Solution 2:[2]

I'm sure there is a built-in feature to parse a composite string like the one you posted, but as I don't know, I've created a CustomParse class to do the job:

class CustomParser():
    def __init__(self, line: str, delimiter: str):
        self.line = line
        self.delimiter = delimiter

    def split(self):
        word = ''
        words = []
        inside_string = False

        for letter in line:
            if letter in '“”"':
                inside_string = not inside_string
                continue
            if letter == self.delimiter and not inside_string:
                words.append(word.strip())
                word = ''
                continue
            word += letter
        words.append(word.strip())

        return words


with open('people_data.csv') as file:
    ages = []
    for line in file:
        ages.append(CustomParser(line, ',').split()[2])
    print(max(ages[1:]))

Hope that helps.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Ralph Macêdo