Python tablib module and pythontablib Module

Source: Internet
Author: User

Python tablib module and pythontablib Module

The Python tablib module is a third-party module. It mainly exports data to different formats, including excel, json, html, yaml, csv, tsv, and other formats, A little exciting. Of course, this module is also super simple to use.


First install this module, open cmd, and then python-m pip install tablib.


After installation, you can use it. The following is a simple example of how to use it.


Import tablibimport OS # create dataset, method 1dataset1 = tablib. dataset () header1 = ('id', 'name', 'tel', 'age') dataset1.headers = header1 dataset1.append ([1, 'hangsan', 13711111111, 16]) dataset1.append ([2, 'lisi', 13811111111, 18]) dataset1.append ([3, 'wangwu', 13911111111, 20]) dataset1.append ([4, 'zhaoliu', 15811111111, 25]) print ('dataset1: ', OS. linesep, dataset1, OS. linesep) # create a dataset, method 2header2 = ('id', 'name', 'tel ', 'age') data2 = [[1, 'zhangsan', 13711111111, 16], [2, 'lisi', 13811111111, 18], [3, 'wangwu', 13911111111, 20], [4, 'zhaoliu', 15811111111, 25] dataset2 = tablib. dataset (* data2, headers = header2) print ('dataset2: ', OS. linesep, dataset2, OS. linesep) # Add a row dataset1.append ([5, 'sunqi', 15911111111, 30]) # Add it to dataset1.insert (0, [0, 'liuyi', 18211111111, 35]) # Add the row print ('dataset1: ', OS. linesep, dataset1, OS. linesep) # delete a row dataset1.pop () # Delete the last row dataset1.lpop () # Delete the first row del dataset1 [0: 2] # Delete the row) print row data ('dataset1: 'After the row is deleted, OS. linesep, dataset1, OS. linesep) # Add a column # currently, dataset1 has two rows of data: dataset1.append _ col ('beijing', 'shenzhen '), header = 'city ') # Add the column to the last column dataset1.insert _ col (2, ('male', 'female '), header = 'sex ') # Add print ('dataset1: ', OS. linesep, dataset1, OS. linesep) # Delete the column del dataset1 ['tel'] print ('dataset1: ', OS. linesep, dataset1, OS. linesep) # obtain print ('yaml format: ', OS. linesep, dataset1.yaml, OS. linesep) print ('csv format: ', OS. linesep, dataset1.csv, OS. linesep) print ('tsv format: ', OS. linesep, dataset1.tsv, OS. linesep) # export to the Excel table dataset1.title = 'dataset1' # Set the name of the Form in Excel dataset2.title = 'dataset2' myfile = open('mydata.xlsx', sheet) myfile. close () # If multiple sheet forms exist, you can use DataBook to set myDataBook to tablib. databook (dataset1, dataset2) myfile = open (myfile. name, 'wb'{myfile.write(mydatabook.xlsx) myfile. close ()

The output result is:

Dataset1: ID | Name | Tel | Age -- | -------- | ----------- | --- 1 | zhangsan | 13711111111 | 16 2 | lisi | 13811111111 | 18 3 | wangwu | 13911111111 | 20 4 | zhaoliu | 15811111111 | 25 dataset2: ID | Name | Tel | Age -- | -------- | ----------- | --- 1 | zhangsan | 13711111111 | 16 2 | lisi | 13811111111 | 18 3 | wangwu | 13911111111 | 20 4 | zhaoliu | 15811111111 | 25 dataset1 after adding a row: ID | Name | Tel | Age -- | -------- | ----------- | --- 0 | liuyi | 18211111111 | 35 1 | zhangsan | 13711111111 | 16 2 | lisi | 13811111111 | 18 3 | wangwu | 13911111111 | 20 4 | zhaoliu | 15811111111 | 25 5 | sunqi | 15911111111 | 30 dataset1 after deleting a row: ID | Name | Tel | Age -- | ------- | ----------- | --- 3 | wangwu | 13911111111 | 20 4 | zhaoliu | 15811111111 | 25 dataset1 after adding columns: ID | Name | sex | Tel | Age | city -- | ------- | ------ | ----------- | --- | -------- 3 | wangwu | male | 13911111111 | 20 | beijing 4 | zhaoliu | female | 15811111111 | 25 | dataset1 after the shenzhen column is deleted: ID | Name | sex | Age | city -- | ------- | ------ | --- | -------- 3 | wangwu | male | 20 | beijing 4 | zhaoliu | female | 25 | shenzhen yaml format: -{Age: 20, ID: 3, Name: wangwu, city: beijing, sex: male}-{Age: 25, ID: 4, Name: zhaoliu, city: shenzhen, sex: female} csv format: ID, Name, sex, Age, city3, wangwu, male, 20, beijing4, zhaoliu, female, 25, shenzhen tsv format: bytes

How is it? Is it very simple ~~~





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.