如何在函数中使用变量创建yaml文件?

2024-10-08 22:23:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python完全陌生,正在尝试为工作编写脚本。以下是一个示例片段:

config.yml:

company1:
    threshold: 3
    alert:
        alert1: 0
        alert2: 0
company2:
    threshold: 6
    alert:
        alert1: 1
        alert2: 1

我的Python脚本:

import yaml
from box import Box

with open("config.yml", 'r') as open_file:
    try:
        cfg = Box(yaml.safe_load(open_file))
    except yaml.YAMLError as err:
        print(err)

def myDef(foo):
    t = cfg.foo.threshold
    a1 = cfg.foo.alert.alert1
    
    return t, a1
    
v = myDef("company1")
print(v[0])
#desired result: 3

x = myDef("company2")
print(x[1])
#desired result: 1

目前,我得到一个错误: BoxKeyError: "'Box' object has no attribute 'foo'"

非常感谢您的帮助


Tags: 脚本boxconfigyamlthresholdfooymlalert
2条回答
ArrayList<String> arrstatus = new ArrayList<String>();
        arrstatus.add("1");
        arrstatus.add("2");
        arrstatus.add("3");
arrstatus.add("4");

for(int j=0;j< c.length;j++)
     {

        if(arrstatus.get(j).equals("1"))
         {
             arrstatus_old.set(j, "Saved");
         }
        else if(arrstatus.get(j).equals("2"))
        {
            arrstatus_old.set(j, "Assigned");
        }
        else if(arrstatus.get(j).equals("3"))
        {
            arrstatus_old.set(j, "Accepted");
        }
        else if(arrstatus.get(j).equals("4"))
        {
            arrstatus_old.set(j, "Rejected");
        }

     }

你的ArrayList存储了1numbersArrayList应该以String格式存储数字

你的arrayList应该像ArrayList<String>

        ArrayList<String> arrstatus = new ArrayList<String>();
        arrstatus.add("1");
        arrstatus.add("2");
        arrstatus.add("3");

当您使用equals签入arraylist时,您可以将数字作为字符串添加

if (arrstatus.get(i).equals("1")) {
    arrstatus.set(i, "Saved");
}if (arrstatus.get(i).equals("2")) {
    arrstatus.set(i, "Assigned");
}if (arrstatus.get(i).equals("3")) {
    arrstatus.set(i, "Accepted");
}

等等

相关问题 更多 >

    热门问题