2021-11-10

2021-11-10,第1张

2021-11-10

字符串直接转换成其他类型
  • 稀奇古怪的冷知识
    • ast库

稀奇古怪的冷知识 ast库

最近需要使用docker进行传参但传进来的都是字符串类型,因此找到这个方法进行转换。
docker传的参数:

docker run -e task_id="4e051590122007024cfb296d9be2ada7" -e task_param='{"tag":"textrcnn","batch_size":32,"num_epochs":1,"learning_rate":0.0001,"pad_size":16,"dropout":0.5}' -e dataset_url="https://tilake.wengegroup.com/wos/zs-a3efde7e/dataset.zip" -e odb_url="http://192.168.10.179:31819/tbase-wos-service/wos/api/v1/upload" -e odb_bucket="zs-a3efde7e" -e callback_url="http://192.168.10.155:30004/ai/callback/ret" -e label_list=["bedroom","CALsuburb","industrial","kitchen","livingroom","MITcoast","MITforest","MIThighway","MITinsidecity","MITmountain","MITopencountry","MITstreet","MITtallbuilding","PARoffice","store"] --name train_cls_container_textrcnn f25

后面的task_param,label_list都是字典类型,因此需要转换
使用如下:

task_id = os.environ['task_id']
task_param = ast.literal_eval(os.environ['task_param'])
dataset_url = os.environ['dataset_url']
odb_url = os.environ['odb_url']
odb_bucket = os.environ['odb_bucket'] 
callback_url = os.environ['callback_url']
label_list = ast.literal_eval(os.environ['label_list'])

总结:

import ast
str_ = '["bedroom","CALsuburb","industrial","kitchen","livingroom","MITcoast","MITforest","MIThighway","MITinsidecity","MITmountain","MITopencountry","MITstreet","MITtallbuilding","PARoffice","store"]'
print(type(str_))
# 
list_ = ast.literal_eval(str_)
print(list_)
# ['bedroom', 'CALsuburb', 'industrial', 'kitchen', 'livingroom', 'MITcoast', 'MITforest', 'MIThighway', 'MITinsidecity', 'MITmountain', 'MITopencountry', 'MITstreet', 'MITtallbuilding', 'PARoffice', 'store']
print(type(list_))
# 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5435706.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存