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)表示第二行第三列的数据,也就是:备注

    (未完,待续)

  • 相关阅读:
    DDD 领域驱动设计-谈谈 Repository、IUnitOfWork 和 IDbContext 的实践
    UVA10071 Back to High School Physics
    UVA10071 Back to High School Physics
    UVA10055 Hashmat the Brave Warrior
    UVA10055 Hashmat the Brave Warrior
    UVA458 The Decoder
    UVA458 The Decoder
    HDU2054 A == B ?
    HDU2054 A == B ?
    POJ3414 Pots
  • 原文地址:https://www.cnblogs.com/dvbbs2012/p/5462747.html
Copyright © 2011-2022 走看看