Selenium2+python Automation 58-reading Excel data (XLRD)

Source: Internet
Author: User

Objective

When there are multiple login accounts, we generally use Excel to store test data, this lesson introduced, Python read the Excel method, and save as a dictionary format.

First, the Environment preparation

1. First install the XLRD module, open cmd, enter pip install xlrd online installation

>>pip Install XLRD

Second, the basic operation

1.exlce Basic Operation method is as follows

# Open Exlce table, parameter is file path
data = Xlrd.open_workbook (' test.xlsx ')

# table = data.sheets () [0] # obtained by index order
# table = Data.sheet_by_index (0) # get through index order
Table = data.sheet_by_name (U ' Sheet1 ') # Gets by name

nrows = table.nrows # gets Total rows
Ncols = table.ncols # Gets the total number of columns

# Gets the value of one row or column, the first row of the argument
Print Table.row_values (0) # Get the first row of values
Print Table.col_values (0) # Get the first column value

Third, Excel store data

1. Storing data in Excel, the first action title, that is, the key value in the corresponding dictionary, such as: Username,password

2. If you have a pure number in your Excel data, you must right-click the format format of the cell, or the data you read is floating point

(Set cell format after editing, edit success in the upper left corner has a small triangle icon)

Four, package Read method

1. The final data read is the list type data of multiple dictionaries, the first row of data is the key value in the dictionary, and the value values are corresponding to the second line.

2. The code after encapsulation is as follows

# Coding:utf-8
Import xlrd
Class Excelutil ():
def __init__ (self, Excelpath, sheetname):
Self.data = Xlrd.open_workbook (Excelpath)
self.table = Self.data.sheet_by_name (sheetname)
# get first row as key value
Self.keys = self.table.row_values (0)
# Get Total rows
Self.rownum = Self.table.nrows
# Gets the total number of columns
Self.colnum = Self.table.ncols

def dict_data (self):
If Self.rownum <= 1:
Print ("Total number of rows less than 1")
Else
r = []
J=1
For I in Range (self.rownum-1):
s = {}
# take the corresponding values from the second row
Values = Self.table.row_values (j)
For x in range (Self.colnum):
S[SELF.KEYS[X]] = values[x]
R.append (s)
J+=1
Return r

if __name__ = = "__main__":
filepath = "D:\\test\\web-project\\5ke\\testdata.xlsx"
SheetName = "Sheet1"
data = Excelutil (filepath, sheetname)
Print Data.dict_data ()

Operation Result:

[{u ' username ': U ' python\u7fa4 ', U ' password ': U ' 226296743 '},

{u ' username ': U ' selenium\u7fa4 ', U ' password ': U ' 232607095 '},

{u ' username ': U ' appium\u7fa4 ', U ' password ': U ' 512200893 '}]

The study process has encountered the question, may add selenium (Python+java) QQ Group Exchange: 232607095

Feel that you have help, in the lower right corner of a praise it, thank you support!

Selenium2+python Automation 58-reading Excel data (XLRD)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.