zoukankan      html  css  js  c++  java
  • python读取txt、csv和excel文件

    一、python读取txt文件:(思路:先打开文件,读取文件,最后用for循环输出内容)

    fp = open('test.txt','r')

    lines = fp.readlines()

    fp.close()

    for line in lines:

        username = line.split(',')[0]

        password = line.split(',')[1]

    注:第一句是以只读方式打开文本文件;第二个是读取所有行的数据(read:读取整个文件;readline:读取一行数据);最后一定要关闭文件。最终会返回一个列表,通过for循环可以一个个的读取其中的数据。如username,password。这时候通过split方法进行分割,最终可以得到username    password,这样就可以在自动化里面做参数化了。

    二、python读取CSV文件

    import csv
    date =csv.reader(open('test.csv','r'))
    for test in date:
        print test

        print test[0]

    注:需要先导入csv包,然后通过reader方法读取内容,最终会返回一个列表。想选择某一列数据,只需要制定列表下标即可

    三、python读取excel

    需要先安装xlrd模块

    账号 密码 备注

    import xlrd

    book=xlrd.open_workbook(data_dirs()+'/system.xlsx')

    sheet=book.sheet_by_index(0)
    print sheet.cell_value(0,2)

    注:(0,2)表示第二行第三列的数据,也就是:备注

    (未完,待续)

  • 相关阅读:
    Net Core -- 配置Kestrel端口
    NET Core迁移
    NET Core 2.0 微服务跨平台实践
    NET Core 与 Vue.js 服务端渲染
    Varnish 实战
    Hitchhiker 是一款开源的 Restful Api 测试工具
    ABP框架用Dapper实现通过SQL访问数据库
    开源框架总体介绍
    Net Core API网关Ocelot
    Jquery autocomplete插件
  • 原文地址:https://www.cnblogs.com/dvbbs2012/p/5462747.html
Copyright © 2011-2022 走看看