python - looping through dictionary of inconsistent lists -
i parsing xml , converting dictionary. there dictionary
'directories': {'dirname': 'd:\\directory\\subdir'},
or
'directories': { 'dirname': [ 'd:\\directory1\\subdir1', 'd:\\directory2\\subdir2']},
how loop through dictionary. doing works if has multiple elements. should check if dirname
list of elements or element , there looping through ? or there pythonic way achieve this
rules = [] v in args["directories"]["dirname"]: rules.append(os.path.join(v, "*", "*"))
output: input 'directories': { 'dirname': [ 'd:\\directory1\\subdir1', 'd:\\directory2\\subdir2']},
rules[] = ['d:\\directory1\\subdir1\\*\\*', 'd:\\directory1\\subdir1\\*\\*']
for input 'directories': {'dirname': 'd:\\directory\\subdir'},
['d\\*\\*', ':\\*\\*', '\\*\\*', 'd\\*\\*', 'i\\*\\*', 'r\\*\\*', 'e\\*\\*', 'c\\*\\*', 't\\*\\*', 'o\\*\\*', 'r\\*\\*', 'y\\*\\*', '\\*\\*', 's\\*\\*', 'u\\*\\*', 'b\\*\\*', 'd\\*\\*', 'i\\*\\*', 'r\\*\\*']
please see me novice in python
you can check type
of args["directories"]["dirname"]
, treat accordingly:
rules = [] dirnames = args["directories"]["dirname"] if isinstance(dirnames, basestring): rules.append(os.path.join(dirnames, "*", "*")) elif isinstance(dirnames, list): v in dirnames: rules.append(os.path.join(v, "*", "*"))
Comments
Post a Comment