Tablib module using a small ape circle of python

There are a lot of python modules because these modules package, make the code more elegant, more powerful, less complicated programming, small ape circle Gaga said today about tablib module What are the features and what can be achieved.

Tablib action module: format conversion (for example: work demand, to convert the file to excel file format json)

Module official website URL: https://pypi.python.org/pypi/tablib/0.9.3

System Environment: centos 7

1, the installation module tablib

wget ttps://bootstrap.pypa.io/get-pip.py
python get-pip.py
pip instal tablib

2, test
#! / Usr / bin / env python

-- coding: utf-8 --

import tablib

headers = (“name”,“age”,“weight”)
data = [(“alex”,“25”,“60kg”),(“xiaoyuanquan”,“18”,“80kg”),(“peiqi”,“18”,“60kg”)]
data.append((“jiajia”,“22”,“47kg”))

data = tablib.Dataset(*data,headers=headers)

print data.json

open(‘xiaoyuanquan.xls’, ‘wb’).write(data.xls)

3, json file export to excel code is as follows:
! # / Usr / bin / env Python

-- coding: utf-8 --

import tablib

data = tablib.Dataset()

f = open(‘alex.json’, ‘rb’)
d = f.read()
f.close()
print d
data.json = d
open(‘xiaoyuanquan.xls’, ‘wb’).write(data.xls)

tablib module will introduce over, is not feeling the format conversion python is very simple and very easy to convert json file to excel format, small series summed up according to alex small ape circle speaking of python, if written in c ++ is definitely a lot of line Code, python very elegant it, this is its powerful place.

Guess you like

Origin blog.csdn.net/weixin_44867000/article/details/91362775