python使用yaml库读取和写入数据

news/2024/7/5 15:16:59
import yaml

test.yml文件数据:


yaml_path = r"D:\PycharmProjects\2020study\BBBstudy\test.yml"
with open(yaml_path, 'r') as f:
    cfg = f.read()
    print(cfg)
    # 此时读出来的数据是字符串
    print(type(cfg))
    # 用yaml.load方法转字典
    d = yaml.load(cfg)
    print(d)
    print(type(d))
输出:


# yaml文件写入
file_path = r"D:\PycharmProjects\2020study\BBBstudy\data.yml"
a = {'name': 'chenchen',
     'race': 'Human',
     'traits': ['Two_Hand', 'Two_Eye']
     }
# 写入到yaml文件
with open(file_path, "w", encoding="utf-8") as f:
    yaml.dump(a, f)
文件中的数据:


http://www.niftyadmin.cn/n/4388300.html

相关文章

lambda函数复习

def test(x):return x*x # 这里我们用lambda定义一个匿名的内联函数。请注意,该函数没有名称。test 是保存创建的lambda函数的变量的名称 # 上面的函数和下面的lambda函数是一样的 test_f lambda x: x*x print(test(3)) print(test_f(3)) # 常量函数 def constant(…

java serializable用法_java基础----gt;Serializable的使用

Serializable序列化的简要说明一、 持久化的简单介绍:“持久化”意味着对象的“生存时间”并不取决于程序是否正在执行——它存在或“生存”于程序的每一次调用之间。通过序列化一个对象,将其写入磁盘,以后在程序再次调用时重新恢复那个对象&…

html5-4 HTML5超链接、URL地址和表格

html5-4 HTML5超链接、URL地址和表格 一、总结 一句话总结&#xff1a; 1、cellspace有什么用&#xff1f; 清除表格的单元格间距 26 <table border1px cellspacing0px width100%> 2、页面中的字体一般用什么好&#xff0c;怎么设置&#xff1f; 微软雅黑&#xff0c…

两行代码激活windows系统

slmgr /skms kms.03k.org slmgr /ato 将以上代码存为bat文件运行

python使用time库创建时间路径和时间文件

import time import osclass FilePath(object):def __init__(self):# 获得当前系统时间的字符串self.local_time time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime(time.time()))print(self.local_time)print(type(self.local_time))# 获取年字符串self.year se…

java mongodb 查询 游标_MongoDB 游标详解及实例代码

MongoDB 游标详解MongoDB中的游标与关系型数据库中的游标在功能上大同小异。游标相当于C语言的指针&#xff0c;可以定位到某条记录&#xff0c;在MongoDB中&#xff0c;则是文档。因此在mongoDB中游标也有定义&#xff0c;声明&#xff0c; 打开&#xff0c;读取&#xff0c;关…

python web自动化测试之二次封装 selenium 类

from logs.logger import Logger from selenium.common.exceptions import NoSuchElementException import time import os from selenium import webdriver """""" """二次封装 selenium 类,又称之为通用类。用于给页面类使用 &…

java程序员昵称_那些神秘的Java程序员

我们都知道程序员的工作主要就是写代码&#xff0c;这样的工作可能会给他们带来近视、秃顶等种种问题。而之前我看过一档辩论型的综艺节目&#xff0c;从中了解到&#xff0c;在北京&#xff0c;女孩们的家长最希望自己未来的女婿是个程序员。这是为什么呢&#xff1f;原来她们…