Are you learning python automated testing? Have you heard of the requests library?
Tablib is a third-party library maintained by the requests library that can manipulate various file formats such as Excel and turn them into a general data set.
The main data formats supported by tablib are:
◆ xls, the excel file format of the old office;
◆ xlsx series, the new office file format;
◆ json
◆ yaml
◆ html
◆ csv
◆df, pandas DataFrame, need to install pandas
In other words, tablib can convert data in different formats into a common relational data format, and then seamlessly switch between the formats.
01. Relational data format
What is the relational data format?
such as:
◆ MySQL data, each row of data corresponds to a field name. You can easily save the data queried from the database into Excel through this library.
◆ Excel data, each row of data has a header.
◆ json, data is divided into key and value
◆ All of the above are similar.
The general data format feature of tablib solves some of the problems of some previous Excel operation libraries.
02, operate Excel common libraries
Several commonly used libraries for operating Excel:
◆ xlrd
◆ openpyxl
These libraries are excellent libraries, and there are limitations:
1. The problem with xlrd. He can only read, not write. To write, you have to install another library.
2. The problem with openpyxl. Only supports reading and writing of xlsx series. Because this Excel format is mostly used now, this problem can actually be ignored, but if your office software is relatively old, you can only use the xls format, this library will not be used.
03, the installation of tablib
Installation of tablib:
1. Install pip install tablib
2. Import import tablib
04, core concepts
Dataset quick start
Let's look at a small example:
◆ url
◆ method
◆expected
url, method, expected is the header of a table. In fact, it is the name of each column. We call it the field name in the database. The following rows are all data.
To create an Excel is very simple, just prepare the data you need, the header of the table and the name of the table title:
The printed result is like this:
05, save as excel
To save as an excel document, you only need to read and write like a normal file, and write the data in the data.xls or data.xlsx properties:
Note: The mode needs to use binary mode wb
The result after saving is like this:
06、Databook
Databook is a concept similar to a workbook, so the parameters required by Databook are actually the DataSet mentioned above.
If there is only one table in an Excel, DataSet is enough. If there are multiple tables:
Table import: import_set
After talking about the Excel write operation, the next is the read operation. Reading an excel file is similar to reading and writing an ordinary file. Call the import_set method:
07, get data
Obtain data for automated testing
Use an example for actual combat, get the data in the excel file, and execute the automated test program:
08, import book
The operation is the same as the DataSet:
09. Summary
In this article, we are familiar with the core concepts of tablib:
◆ DataBook
◆ DataSet
◆ Read, import_set, import_book
◆ write
In the next part, we will deal with ranks and data operations, as well as some of its characteristics.
Then share some of my favorite information below, I hope it can help everyone
This information is organized around [software testing]. The main content includes: python automated testing exclusive video, Python automated detailed information, a full set of interview questions and other knowledge content. For friends of software testing, it should be the most comprehensive and complete preparation warehouse. This warehouse has accompanied me through a lot of bumpy roads, and I hope it can also help you. Pay attention to WeChat public account: Programmer Erhei, you can get it directly