I have a text file that contains list of files, example line: "drwx------ 8 foo bar-all 4096 Dec 27 13:55 .git"
The regex is correct and findall works as expected. The var ´matches´ is a list of tuples that have strings representing numbers and other strings inside them.
How can I typecast all the strings representing numbers to integers without losing the tuples?
The below code returns a list with the tuples no longer there 😦
#!/usr/bin/env python3
import re
def file_listing(filename="src/listing.txt"):
with open(filename, "r") as file:
contents = file.read()
pattern = (
r"([\d]*)\s([A-Z][a-z]+)\s([\d]+)\s([\d]+)\:([\d]+)\s(.[a-z]*[.]?[a-z]*)"
)
matches = re.findall(pattern, contents)
print(matches) # [('2356', 'Dec', '11', '11', '50', 'add'), ('164519', 'Dec', '28', '17', '59', 'basics.ipynb')]
return [(int(j) if j.isdigit() else j) for i in matches for j in i]
def main():
print(file_listing("src/listing.txt"))
if __name__ == "__main__":
main()