I have a large number of images that were named in a odd way and because of that, when they're displayed in a file browser, they're completely out of order. However, since they weren't named in a completely random manner, I can use regex to properly rename them, once I understand all the subpatterns that went into it. So far I only understood two.
So I'm going to create a program to verify the pattern of each filename, make a set out of it with a sample of each name, and then try to understand the patterns and subpatterns.
def get_pattern(name):
pattern = []
for letter in name:
if letter.isalpha():
pattern.append('a')
elif letter.isdigit():
pattern.append('0')
else:
pattern.append(letter)
return ''.join(pattern)
It's pretty straightforward to loop through the images and then check the pattern for each image.
My questions:
- Is there a way to make the code more concise? Maybe using a dictionary?
- Is there something in
re
or other easily available library that does this? Is usingisalpha()
andisdigit()
the way to go here?
map
or a comprehension on name with a function that handles with a single letter but this seems fairly ok to me. \$\endgroup\$''.join(['a' if l.isalpha() else '0' if l.isdigit() else l for l in name])
, but your code is fairly clear as it is now. You could do away with constructing a list and just append characters to a string. \$\endgroup\$