简体   繁体   English

如何将存储在变量中的函数结果存储为字符串?

[英]How can I store the result of a function stored in variable as a string?

I'm working on an open source script, for AWS snapshots using the boto3 python sdk. 我正在使用boto3 python sdk开发用于AWS快照的开源脚本。

I have a function that returns json, which contains a function: 我有一个返回json的函数,其中包含一个函数:

datetime.datetime(2017, 11, 3, 21, 2, 27, tzinfo=tzlocal())

When I store the results from the json, it stores that function string, rather than the result of that function. 当我存储json的结果时,它将存储该函数字符串,而不是该函数的结果。

How can I get python3 to execute that function? 如何获取python3执行该功能?

(oddly enough when I print that dict time, it shows correct) (当我打印该字典时间时,这很奇怪,它显示正确)

This is what my dict looks like: 这是我的字典的样子:

{'snap-05c84': datetime.datetime(2017, 11, 3, 22, 4, 48, tzinfo=tzlocal()), 'snap-08bcb': datetime.datetime(2017, 11, 3, 21, 2, 27, tzinfo=tzlocal())}

And the code that builds the dict is: 构建字典的代码是:

for snap in snaps['Snapshots']:
  snap_id=snap['SnapshotId']
  start_time=snap['StartTime']
  snap_times[snap_id]=start_time

Thanks in advance!!! 提前致谢!!!

You can use eval, to execute code, and get result of it. 您可以使用eval执行代码并获取结果。

However, as I'm looking at your example, why you have that datetime object as string? 但是,当我查看您的示例时,为什么将那个datetime对象作为字符串? Probably you wish to have value already there. 也许您希望在那里已经拥有价值。 You can use handler for that: 您可以为此使用处理程序:

import datetime
import json

def datetime_handler(x):
    if isinstance(x, datetime.datetime):
        return x.isoformat()
    raise TypeError("Unknown type")

json.dumps(data, default=datetime_handler)

If you don't like isoformat - strftime can be used. 如果您不喜欢isoformat,则可以使用strftime

If I am understanding you correctly, you have a function name that is being stored as a string. 如果我对您的理解正确,那么您有一个函数名称,该名称存储为字符串。 If that is the case, you can use solutions provided here Calling a function of a module from a string with the function's name to use that function. 如果是这种情况,您可以使用此处提供的解决方案,从带有函数名称的字符串中调用模块的函数以使用该函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将某些内容附加到函数的结果中并将其存储在python中的变量中 - How can I append something to a result of a function and store that in a Variable in python 如何在Python中调用带有存储为字符串的变量的函数 - How in Python can I call a function with a variable which is store as a string 如何修补存储在变量中的函数? - How can I patch a function stored in a variable? 如何存储通过“ for statement”变量获得的打印结果? - how can i store the printing result, obtained by “for statement” variable? 如何将中间结果存储在 pyspark reduceByKey 函数中? - how can I store the intermediate result in pyspark reduceByKey function? 将函数结果存储到变量中 - Store function result into variable 将函数的结果存储为变量 - Store the result of a function as a variable 如何从我制作的存储在主程序中的全局变量中的函数中获取字符串? - How can I get a string from a function I made to be stored in a global variable I have in my main program? 如何从也存储为变量的 python 文件中调用存储为变量的 function? - How can I call a function that is stored as a variable from a python file that is also stored as a variable? 如果我使用 timeit 计时需要多长时间,如何将函数的结果存储在变量中? - How do I store the result of a function in a variable if I'm timing how long it takes using timeit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM