首页 > 编程语言 >python小知识

python小知识

时间:2023-08-28 23:55:25浏览次数:27  
标签:old uuid python 知识 headers print new data

Python小知识

浮点数注意事项

# 浮点数精度问题
from decimal import Decimal

print(0.1 + 0.1)  # 0.2
print(0.1 + 0.2)  # 0.30000000000000004

print(Decimal("0.1") + Decimal("0.2"))  # 0.3,注意:参数是字符串

jsonpath提取数据

# pip install jsonpath
from jsonpath import jsonpath

actual = {"data": {"msg": "The verification code is wrong or expired"}, "code": 200}

expected = {"msg": "The verification code is wrong or expired", "code": 200}
for key, value in expected.items():
    print(jsonpath(actual, f"$..{key}"))  # 以列表展示
    print(jsonpath(actual, f"$..{key}")[0])
    assert jsonpath(actual, f"$..{key}")[0] == expected[key]

expected = {"$..msg": "The verification code is wrong or expired", "$..code": 200}
for key, value in expected.items():
    actual_value = jsonpath(actual, key)[0]
    print(actual_value)
    assert actual_value == value

数据替换

import json
import string

# 数据替换
old_headers = '{"Authorization": "new token", "uuid": "new uuid"}'
headers = json.loads(old_headers)
headers["Authorization"] = "adifhsjfnaksldf"
headers["uuid"] = "xxx"
print(headers)

# 新的办法
# old_headers = '{"Authorization": "${token}", "uuid": "${uuid}", "a": "b"}'
old_headers = '{"Authorization": "${token}", "uuid": "old uuid", "a": "${b}"}'
new_data = {"token": "my token", "uuid": "my uuid", "b": "new c"}
t = string.Template(old_headers)
new_headers = t.substitute(new_data)
print(new_headers)


# 封装替换数据的函数
def replace_data(old: str, new: dict):
    t = string.Template(old)
    new_headers = t.substitute(new)
    return new_headers


if __name__ == '__main__':
    print(replace_data(old_headers, new_data))

    old_headers = '{"Authorization": "token", "uuid": "old uuid", "a": "b"}'  # 替换的数据为字符串类型
    new_data = {}
    # 数据替换的用法,如果old_headers没有需要替换的数据,new_data数据为空,得到的new_headers为old_headers数据
    t = string.Template(old_headers)
    new_headers = t.substitute(new_data)
    print(new_headers)
    print(type(new_headers))  # 生成的也是字符串类型化
    print(json.loads(new_headers))  # 转换成字典类型的数据
    print(type(json.loads(new_headers)))

yaml学习

  • 支持的数据类型:字典、列表、字符串、布尔值、整数、浮点数、Null、时间等

  • 基本语法规则:

    • 大小写敏感
    • 使用缩进表示层级关系
    • 相同层级的元素左侧对齐
    • 键值对用冒号 “:” 结构表示,冒号与值之间需用空格分隔
    • 数组前加有 “-” 符号,符号与值之间需用空格分隔
    • None值可用null 和 ~ 表示
    • 多组数据之间使用3横杠---分割
    • #表示注释,但不能在一段代码的行末尾加 #注释,否则会报错

读取yaml文件

# 注释
# 后面的字符串表示建议带引号
# 冒号后面一定要空格
name: "yu"
name1: yu
age: 26

femail: true

# python中的字典
database: { "host": "http://xxx.com", "port: 3306" }
database1:
  host: "http://xxx.com"
  port: 3306

# 列表
hobby: [ "music", "电影" ]
hobby1:
  - "music"
  - "电影"
# pip install pyyaml
import yaml

# 读取yaml
with open("read.yaml", encoding="utf-8") as f:
    # print(f.read())
    data = yaml.safe_load(f)
    print(data, type(data))
# 结果:
# {'name': 'yu', 'name1': 'yu', 'age': 26, 'femail': True, 'database': {'host': 'http://xxx.com', 'port: 3306': None}, 'database1': {'host': 'http://xxx.com', 'port': 3306}, 'hobby': ['music', '电影'], 'hobby1': ['music', '电影']} <class 'dict'>
# pip install pyyaml
import yaml

# 读取yaml
with open("read.yaml", encoding="utf-8") as f:
    result = yaml.load(f, Loader=yaml.FullLoader)
    print(result, type(result))
# 结果:
# {'name': 'yu', 'name1': 'yu', 'age': 26, 'femail': True, 'database': {'host': 'http://xxx.com', 'port: 3306': None}, 'database1': {'host': 'http://xxx.com', 'port': 3306}, 'hobby': ['music', '电影'], 'hobby1': ['music', '电影']} <class 'dict'>

标签:old,uuid,python,知识,headers,print,new,data
From: https://www.cnblogs.com/HollowPan/p/17663693.html

相关文章

  • python+selenium+pytest-(5)_yaml文件
    导包importyamlelement.yamllogin:safe:'#element'link:'#element'user:['#element','user1','user2','user3']password:['#element','pw']auth_code:'element......
  • python代码画爱心❤(海龟)
    importturtle#设置标题turtle.title("蜜蜂的程序")turtle.st()#显示海龟print(turtle.position())turtle.color("red","pink")turtle.begin_fill()#填充前turtle.left(90)turtle.penup()turtle.pendown()turtle.circle(60,180)turtle.circle(18......
  • python的print和input的使用
    input前面引号里面包含的字符串会先调用print语句#1.使用input函数分别获得用户输入的个人信息#2.个人信息包含姓名,年龄,性别,爱好,职业信息等#3.使用print函数将输入的结果打印出来name=input("请输入姓名:")age=input("请输入年龄:")sex=input("请输入性别:")hobby=inp......
  • python中计算dna序列的GC含量
     001、对G、C计数进行统计[root@pc1test01]#lsa.fatest.py[root@pc1test01]#cata.fa##测试DNA序列>Rosalind_6404CCTGCGGAAGATCGGCACTAGAATAGCCAGAACCGTTTCTCTGAGGCTTCCGGCCTTCCCTCCCACTAATAATTCTGAGG>Rosalind_5959CCATCGGTAGCGCATCCTTAGTCCAATTAAG......
  • python+playwright 学习-79 设置全局导航超时和全局查找元素超时
    前言playwright默认全局的导航时间是30秒,查找元素超时也是30秒,有以下几个方法设置全局超时时间:browser_context.set_default_navigation_timeout()browser_context.set_default_timeout()page.set_default_navigation_timeout()page.set_default_timeout()导航超时设置......
  • python中输出键最大、最小的项
     001、输出键最大的项a、>>>dict1={"c":30,"a":40,"b":80,"d":20,"e":60}>>>dict1{'c':30,'a':40,'b':80,'d':20,'e':60}>>&......
  • Python学习总结:类属性、类方法、self、cls
    转载:Python学习总结(五)类属性、类方法、self、cls_摩霄志在潜修羽的博客-CSDN博客......
  • Python基础
    第1章 Python基础知识一、Python简介1、Python的起源创始人:吉多范罗苏姆,1989年圣诞节前在社交平台发布第一个解释器:1991年使用C语言编写,会自带C语言的异常处理和函数2、python语言的特点(1)简单(2)面向对象(3)开源、免费3、学习的目的Python是基础语法,主要是为了写自动化脚本打基础4、......
  • python中输出字典中值最大或最小的项
     001、输出值最大的项a、>>>dict1={"c":30,"a":40,"b":80,"d":60}##测试字典>>>dict1{'c':30,'a':40,'b':80,'d':60}>>>max_value=max(dict......
  • windows中Python安装
      下载地址:https://www.python.org/downloads/windows/选择需要的版本,我下载的是3.10安装时,注意选择对所有用户安装,否则安装后,运行项目报“CreateProcesserror=5,拒绝访问”错误安装时,选择AddPythontoenvironmentvariables复选框时,会自动帮我们配置环......