1

I'm trying to create repaired path using 2 dicts created using groupdict() from re.compile

The idea is the swap out values from the wrong path with equally named values of the correct dict. However, due to the fact they are not in the captured group order, I can't rebuild the resulting string as a correct path as the values are not in order that is required for path.

I hope that makes sense, I've only been using python for a couple of months, so I may be missing the obvious.

    # for k, v in pat_list.iteritems():
    #   pat = re.compile(v)
    #   m = pat.match(Path)
    #   if m:
    #       mgd = m.groups(0)
    #       pp (mgd)

this gives correct value order, and groupdict() creates the right k,v pair, but in wrong order.

ekad
  • 14,436
  • 26
  • 44
  • 46

1 Answers1

0

You could perhaps use something a bit like that:

pat = re.compile(r"(?P<FULL>(?P<to_ext>(?:(?P<path_file_type>(?P<path_episode>(?P<path_client>[A-Z]:[\\/](?P<client_name>[a-zA-z0-1]*))[\\/](?P<episode_format>[a-zA-z0-9]*))[\\/](?P<root_folder>[a-zA-Z0-9]*)[\\/])(?P<file_type>[a-zA-Z0-9]*)[\\/](?P<path_folder>[a-zA-Z0-9]*[_,\-]\d*[_-]?\d*)[\\/](?P<base_name>(?P<episode>[a-zA-Z0-9]*)(?P<scene_split>[_,\-])(?P<scene>\d*)(?P<shot_split>[_-])(?P<shot>\d*)(?P<version_split>[_,\-a-zA-Z]*)(?P<version>[0-9]*))))[\.](?P<ext>[a-zA-Z0-9]*))")
s = r"T:\Grimm\Grimm_EPS321\Comps\Fusion\G321_08_010\G321_08_010_v02.comp"
mat = pat.match(s)
result = []

for i in range(1, pat.groups):
    name = list(pat.groupindex.keys())[list(pat.groupindex.values()).index(i)]
    cap = res.group(i)
    result.append([name, cap])

That will give you a list of lists, the smaller lists having the capture group as first item, and the capture group as second item.

Or if you want 2 lists, you can make something like:

names = []
captures = []

for i in range(1, pat.groups):
    name = list(pat.groupindex.keys())[list(pat.groupindex.values()).index(i)]
    cap = res.group(i)
    names.append(name)
    captures.append(cap)

Getting key from value in a dict obtained from this answer

Community
  • 1
  • 1
Jerry
  • 70,495
  • 13
  • 100
  • 144