Home > Software engineering >  Typecasting to int while searching for patterns with regex, avoiding immutability of tuples
Typecasting to int while searching for patterns with regex, avoiding immutability of tuples

Time:06-23

I'm currently working on a problem where I read lines from files, with lines formatted as such:

-rw-r--r-- 1 jttoivon hyad-all   25399 Nov  2 21:25 exception_hierarchy.pdf

and the expected output is as list of correlating tuples, for example:

(25399, "Nov", 2, 21, 25, "exception_hierarchy.pdf")

I have accomplished this with regex matching, but my answer looks like:

("25399", "Nov", "2", "21", "25", "exception_hierarchy.pdf")

Is there any way to typecast the number strings to int during regex matching and get around the immutability of tuples? I have gone through my list of tuples and printed out only those number strings in each tuple, so I know I can access them. The problem is I tried to typecast to int and realized that nothing happens. Do I have to somehow copy the entire list of tuples? Seems very inefficient. Here is my code so far:

import re

def file_listing(filename="src/listing.txt"):

    tuple_list = []

    with open(filename, "r") as file:
        for lines in file:
            current_line = file.readline()
            found = re.search(r".* \d .* .* (\d ) (\w ) (\d ) (\d ):(\d ) (.*)", current_line)
            if found:
                found_groups = found.groups()
                for item in found_groups:
                    if item.isdigit():
                        print(item)          # Found the correct items, but...
                        #item = int(item)    # tuples are immutable!! find another way
                tuple_list.append(found_groups)

           

    return tuple_list

def main():
    print(file_listing())
if __name__ == "__main__":
    main()

CodePudding user response:

Tuple is a structure which is unchangeable. You have to create another tuple and add the new one to tuple_list. For example:

tuple_list = []

groups = "25399", "Nov", "2", "21", "25", "exception_hierarchy.pdf"
tuple_list.append(tuple(map(lambda x: int(x) if x.isdigit() else x, groups)))

print(tuple_list)

Prints:

[(25399, 'Nov', 2, 21, 25, 'exception_hierarchy.pdf')]
  • Related