Python tablib模块是第三方模块,主要作用是将数据导出为各种不同的格式,包括excel,json,html,yaml,csv,tsv等格式,怎么样,有点心动了吧,当然这个模块使用起来也是超级简单的
首先安装这个模块,打开cmd,然后python –m pip install tablib就可以啦
安装好之后,就可以使用啦,下面举个简单的栗子就知道怎么用啦
import tablibimport os#创建dataset,方法1dataset1 = tablib.Dataset()header1 = ('ID', 'Name', 'Tel', 'Age')dataset1.headers = header1 dataset1.append([1, 'zhangsan', 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)#创建dataset,方法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)#增加行dataset1.append([5, 'sunqi', 15911111111, 30]) #添加到最后一行的下面dataset1.insert(0, [0, 'liuyi', 18211111111, 35]) #在指定位置添加行print('增加行后的dataset1: ', os.linesep, dataset1, os.linesep)#删除行dataset1.pop() #删除最后一行dataset1.lpop() #删除第一行del dataset1[0:2] #删除第[0,2)行数据print('删除行后的dataset1:', os.linesep, dataset1, os.linesep)#增加列#现在dataset1就剩两行数据了dataset1.append_col(('beijing', 'shenzhen'), header='city') #增加列到最后一列dataset1.insert_col(2, ('male', 'female'), header='sex') #在指定位置添加列print('增加列后的dataset1: ', os.linesep, dataset1, os.linesep)#删除列del dataset1['Tel']print('删除列后的dataset1: ', os.linesep, dataset1, os.linesep)#获取各种格式的数据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)#导出到Excel表格中dataset1.title = 'dataset1' #设置Excel中表单的名称dataset2.title = 'dataset2'myfile = open('mydata.xlsx', 'wb')myfile.write(dataset1.xlsx)myfile.close()#如果有多个sheet表单,使用DataBook就可以了myDataBook = tablib.Databook((dataset1, dataset2))myfile = open(myfile.name, 'wb')myfile.write(myDataBook.xlsx)myfile.close()
输出的结果为:
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: 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: ID|Name |Tel |Age--|-------|-----------|---3 |wangwu |13911111111|20 4 |zhaoliu|15811111111|25 增加列后的dataset1: ID|Name |sex |Tel |Age|city --|-------|------|-----------|---|--------3 |wangwu |male |13911111111|20 |beijing 4 |zhaoliu|female|15811111111|25 |shenzhen 删除列后的dataset1: 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: ID Name sex Age city3 wangwu male 20 beijing4 zhaoliu female 25 shenzhen怎么样,是不是很简单呢~~~