Saving list of many python variables into excel sheet while simultaneously keeping variable types defined? Saving list of many python variables into excel sheet while simultaneously keeping variable types defined? vba vba

Saving list of many python variables into excel sheet while simultaneously keeping variable types defined?


As per your comment, you can get eval to correctly process the symbols that are local to some module by passing the appropriate dict of locals into eval, along with your string. Here's a workable solution:

import pandas as pddef getlocals(obj, lcls=None):    if lcls is None: lcls = dict(locals().items())    objlcls = {k:v for k,v in obj.__dict__.items() if not k.startswith('_')}    lcls.update(objlcls)    return lclsx = "[123,DatetimeIndex(['2018-12-04','2018-12-05', '2018-12-06'],dtype='datetime64[ns]', freq='D')]"lcls = getlocals(pd)result = eval(x, globals(), lcls)print(result)

Output:

[123, DatetimeIndex(['2018-12-04', '2018-12-05', '2018-12-06'], dtype='datetime64[ns]', freq='D')]

As a Responsible Person, it is also my duty to warn you that using eval for your application is ridiculously unsafe. There are many discussions of the dangers of eval, and none of them suggest there's a way to completely mitigate those dangers. Be careful if you choose to use this code.