zoukankan      html  css  js  c++  java
  • selenium10-python3部分代码复习

    写selenium脚本,免不了一个问题就是需要将测试用的数据和功能代码分离,数据作为单独一个文件,功能代码模块在需要时调用数据文件中的数据,于是这里复习一下相关的一部分python3的代码。

    首先是字典的应用,这里分为最简单的字典格式,还有字典嵌套字典的情况,至于列表嵌套字典、字典嵌套列表等情况先不管了, 反正我觉得字典嵌套字典最实用。

    还有就是文件的读写。

    简单字典

    dict1 = {key1: value, key2: value0}  #简单的字典格式,包含两对键值对

    dict1[key3] = value3  #增加键值对

    dict1[key2] = value2  #修改某键对应的值

    del dict1[key3]  #删除键值对

    for key, value in dict1.items():  #遍历所有的键值对

      print(key, value)

    for key in dict1.keys():  #遍历所有的键

      print(key)

    for value in dict1.values():  #遍历所有的值

      prints(value)

    for key in sorted(dict1.keys()):  #按顺序遍历所有键

      print(key)

    嵌套字典

    dict2 = {

    user1: {name: name1, add: add1}

    user2: {name: name2, add: add2}

    user3: {name: name3, add: add3}}

    for key1, value1 in dict2.items():  #遍历字典 dict2 中所有的键值对

      for key2, value2 in value1.items():  #遍历字典 value1 中所有的键值对

        print(key2, value2)

    文件读写

    with open("user", "r") as file_object:

      contents = f.read()

      print(contents)

      #只读模式r,此时参数可以省略

      for line in file_object:

        print(line)

      #逐行读取

      content = file_object.readline()

      while content:

        print(content)

        content = file_object.readline()

      #每次读取一行

      lines =  file_object.readlines()

      #一次读取所有行,返回列表

    with open(filename, 'w') as file_object:  #写入文件

      file_object.write("youzi")

    with open(filename, "a") as file_object:  #附加到文档尾部

      file_object.write("youzi")

    with open("user") as file_object:

      contents = file_object.read()

      words = contents.split(" ")  #生成一个列表,以空格为分隔符,包含所有单词

      num_words = len(words)

    import json

    with open(filename) as file_object:  #将内容写入json文件

      json.dump("dsds", file_object)

    with open(filename) as file_object:  #读取json文件

      msg = json.load(file_object)

  • 相关阅读:
    java — 排序算法
    sping框架(3)— 使用spring容器
    java — 线程池
    一致性哈希算法(consistent hashing)
    linux进程调度
    滑动窗口协议
    final和static关键字
    II 3.1 连接到服务器
    intel-hadoop/HiBench流程分析----以贝叶斯算法为例
    Android开发学习之路--基于vitamio的视频播放器(二)
  • 原文地址:https://www.cnblogs.com/maigeyouziba/p/11252850.html
Copyright © 2011-2022 走看看