python修改yaml文件_python2.7 读取yaml文件

这篇博客介绍了如何使用Python读取和修改yaml文件,包括对象、数组、多个对象的解析,并解决了读取中文显示乱码的问题。示例代码展示了yaml数据结构转换为Python数据结构的过程。
摘要由CSDN通过智能技术生成

前言

之前看了很多帖子,都在使用yaml配置测试用例,一直没有尝试,最近整理openapi脚本,第一次使用了yaml配置接口条件,于是顺便学习了一下yaml语法。总结的不到位的地方,欢迎批评指正。

备注:这篇文章主要参考了阮一峰的文章,这是传送门,以及官方wiki文档

语言介绍部分就不多说了,网上有很多介绍,直接从数据结构开始说吧。

同时记录中文读取显示乱码的解决方法。

对象

这应该算是最简单的一种格式了,就是一组键值对

1. 新建data.yaml文件,内容如下:

name: waiqin365

code: cus001

type: market

脚本如下:

import yaml

f=open('data.yaml')

data = yaml.load(f)

print data

转换为python后:

{'code': 'cus001', 'type': 'market', 'name': 'waiqin365'}

修改yaml文件,格式如下:

name: waiqin365

detail:

code: cus001

type: market

转换为python后:

{'name': 'waiqin365', 'detail': {'code': 'cus001', 'type': 'market'}}

数组

一组连词线开头的行,构成一个数组

1.新建data.yaml,格式如下:

- cus001

- cus002

- cus003

转换为python后:

['cus001', 'cus002', 'cus003']

修改yaml文件,格式如下:

- cus001:

code: 001

- cus002:

code: 002

type: 0022

- cus003

转换为python后:

[{'cus001': {'code': 'code001'}}, {'cus002': {'code': 'code002', 'type': 'tyoe002'}}, 'cus003']

多个对象

1.新建data.yaml,格式如下:

备注:这是官方wiki文档的例子

---

name: The Set of Gauntlets 'Pauraegen'

description:

A set of handgear with sparks that crackle across its knuckleguards.

---

name: The Set of Gauntlets 'Paurnen'

description:

A set of gauntlets that gives off a foul,acrid odour yet remains untarnished.

---

name: The Set of Gauntlets 'Paurnimmen'

description:

A set of handgear, freezing with unnatural cold.

需要修改python脚本,改用load_all来解析文档:

import yaml

f=open('data.yaml')

for data in yaml.load_all(f):

print data

转换为python后:

{'name': "The Set of Gauntlets 'Pauraegen'", 'description': 'A set of handgear with sparks that crackle across its knuckleguards.'}

{'name': "The Set of Gauntlets 'Paurnen'", 'description': 'A set of gauntlets that gives off a foul,acrid odour yet remains untarnished.'}

{'name': "The Set of Gauntlets 'Paurnimmen'", 'description': 'A set of handgear, freezing with unnatural cold.'}

对象和数组结合

1.新建data.yaml,格式如下:

cus:

- cus001

- cus002

- cus003

detail:

type: {type1: a}

dept: dept1

转换为python后:

{'cus': ['cus001', 'cus002', 'cus003'], 'detail': {'dept': 'dept1', 'type': {'type1': 'a'}}}

读取中文显示问题

当data.yaml文件中,含有中文时,读取后显示有问题:

如下,data.yaml文件为:

cus:

- 终端001

- 终端002

- 终端003

detail:

type: {type1: a}

dept: dept1

用上面脚本,直接打印出来结果为:

{'cus': [u'\u7ec8\u7aef001', u'\u7ec8\u7aef002', 'cus003'], 'detail': {'dept': 'dept1', 'type': {'type1': 'a'}}}

如何才能直接将 cus:['终端001','终端002','终端003'] 打印出来呢?

修改脚本如下:

f=open('data.yaml',)

data = yaml.load(f)

print data

print (repr(data).decode('unicode-escape'))

对比一下,打印结果如下:

{'cus': [u'\u7ec8\u7aef001', u'\u7ec8\u7aef002', 'cus003'], 'detail': {'dept': 'dept1', 'type': {'type1': 'a'}}}

{'cus': [u'终端001', u'终端002', 'cus003'], 'detail': {'dept': 'dept1', 'type': {'type1': 'a'}}}

ok问题解决,解决方法,主要参考知乎一片文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值