you could try running your data through a function with default arguments, small hack:
def list_it(name = null, age = null, country = null):
return [name, age, country]
for i in list_it():
if i is not None:
#do something
Question
I have a particular file in which data is stored corresponding to certain labels. For eg:
Name: John; Age: 14; Country: USA; etc
However, it is not consistent i.e the file contains something like
Name: Kevin; Age: 15; etc
Notice that this data point contains some missing attributes like Country.
I wanted to set all the missing data as None. Specifically, something like:
try:
(Do something with the available attributes)
except Exception,e:
(Set all the attributes with error as null)
Thank you in advance.
Solution
you could try running your data through a function with default arguments, small hack:
def list_it(name = null, age = null, country = null):
return [name, age, country]
for i in list_it():
if i is not None:
#do something
OTHER TIPS
My ver:
keyword = [
'Name',
'Age',
'Country',
]
buf = 'Name: Kevin; Age: 15;'
d = dict(item.strip().split(': ') for item in buf.split(';')[:-1])
for k in keyword:
if k in d:
# (Do something with the available attributes)
pass
else:
# (Set all the attributes with error as null)
d[k] = None
print(d)
# {'Country': None, 'Age': '15', 'Name': 'Kevin'}