python - Get content of a string variable using getattr -
i know how retrieve content of string variable can use value argument name in function. here code:
import pandas import jinja2 odatelist = ['2017-03-22','2017-03-23','2017-03-24'] odata = pandas.dataframe() odata['date'] = odatelist mytemplate = 'today {{ date }}' otemplate = jinja2.template(mytemplate) orow in odata.index: ocolumn in odata.columns: mytemplateupdated = otemplate.render(date=odata.loc[orow, ocolumn]) print(mytemplateupdated)
it works , returns:
today 2017-03-22 today 2017-03-23 today 2017-03-24
i dynamically retrieve argument name date=
dataframe column name ocolumn
(which 'date'). thought using getattr(ocolumn, 'something')
did not figure out how so.
i have tried str(ocolumn)
, returns error: syntaxerror: keyword can't expression
thank you
if want dynamically set argument name being sent function, you'll need use kwargs.
render(**{argument_name: argument_value}
so in case, assuming ocolumn contains string wish argument, this;
render(**{ocolumn: odata.loc[orow, ocolumn]})
let me know if have misunderstood intent.
Comments
Post a Comment