w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Given a Python string describing object.attribute, how do I separate the attributes's namespace from the attribute?

Assuming that the namespace is always alphanumeric, you could first split on /[^a-zA-Z.]/, then rsplit on .:

>>> import re
>>> ns_attr_split = lambda s: re.split("[^a-zA-Z.]", s,
>>> ns_attr_split("obj.dict['key.word']") 
['obj', 'dict']

Obviously this isn't exactly what you want… but the fiddling would be straight forward.

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.