python接口自动化测试十:字典、字符串、json之间的简单处理
# 字典
a = None # None = null
b = False # boolean
c, d = 12, 10.6 # int float
e = 'asdd' # str
f = ['s', 'e'] # list,数组,可增删改查
g = ('a', 's', 'f') # 元组(tuple),只能查
# 字典
t = {
# 键:值
'': '',
'': ''
}
# 字典
# dict key: value key是唯一的,无序的
h = {
's': 12,
'f': False,
2: 'd',
'r': 't',
'sd': ['gf', 'rt', ' ew'],
'rt': ('rte', 'ewr'),
'ru': {
'rty': 'fghg',
'dfs': 'yjtujyt'
}
}
# print(h)
print(h['ru']) # 取出指定的key为ru的值
# 取出所有的key
keys = h.keys()
print(keys)
# 遍历所有的key
for i in list(keys):
print('%s = %s'%(i, h[i]))
abc = {'aa': 'dd', 'bb': 'th'}
print(abc)
print(type(abc)) # 查看abc的数据类型
aaa = str(abc) # 字典转str
print(type(aaa))
print(aaa)
bbb = eval(aaa) # str转字典
print(type(bbb))
print(bbb)
# 字符串转字典
bbb = eval(aaa)
print(type(bbb))
print(bbb)
# 字典转json: json本质是字符串,只是按一定规则转换的
d_json = json.dumps(h)
print(d_json)
print(type(d_json))
# json(str)转字典 : 把json格式转换成字典格式
json_dict = json.loads(d_json)
print(type(json_dict))
print(json_dict)