yaml读取和加载及科学计数法表示

yaml的读取和加载

import yaml

def load_yaml(file_path):
    with open(file_path) as f:
        config = yaml.load(f, Loader=yaml.Loader)
    return config

def write_yaml(yaml_dict, file_path):
    new_dict = {}
    new_dict.update(yaml_dict)
    with open(file_path, 'w') as f:
        yaml.dump(new_dict, f, allow_unicode=True, sort_keys=False)

.yaml 文件使用中要注意:
科学计数法的表示,不能直接
1e-5
这样会被识别成字符串,所以正确的写法是
1.0e-5
这样就会识别成浮点数了

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在Unity中,YAML(又名“YAML Ain't Markup Language”)是一种用于存储和序列化数据的格式,常用于配置文件和场景文件。Unity引擎使用YAML格式来保存场景和预制件的信息,你可以通过读取和解析YAML文件来访问这些数据。以下是一些常用的读取YAML文件的方法: 1.使用Unity自带的YAML解析器。Unity提供了YAML解析器类YAMLParser,可以通过该类来读取YAML文件。示例代码如下: ```csharp using UnityEngine; using UnityEditor; using System.IO; public class ReadYamlFile : MonoBehaviour { [MenuItem("Tools/ReadYamlFile")] static void ReadYaml() { string filePath = Application.dataPath + "/test.yaml"; if (File.Exists(filePath)) { string content = File.ReadAllText(filePath); YAMLParser parser = new YAMLParser(); object data = parser.Parse(content); Debug.Log(data); } else { Debug.LogError("File does not exist: " + filePath); } } } ``` 2.使用第三方的YAML解析器。除了Unity自带的YAML解析器外,还有许多第三方的YAML解析器可以使用,例如YamlDotNet和SharpYaml等。这些解析器使用方法类似,你可以选择适合自己的解析器来使用。 ```csharp using UnityEngine; using System.IO; using YamlDotNet.Serialization; public class ReadYamlFile : MonoBehaviour { static void Main(string[] args) { string filePath = Application.dataPath + "/test.yaml"; if (File.Exists(filePath)) { string content = File.ReadAllText(filePath); Deserializer deserializer = new Deserializer(); object data = deserializer.Deserialize(new StringReader(content)); Debug.Log(data); } else { Debug.LogError("File does not exist: " + filePath); } } } ``` 无论使用哪种方法,你都需要了解YAML文件的语法规则,以便正确地读取和解析文件内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值