当前位置: 首页 > 工具软件 > Tablib > 使用案例 >

html python 导出csv,Python Tablib导出数据为Excel、JSON、CSV等格式

毕和志
2023-12-01

Tablib是一个与表格格式有关的Python库,支持以下导出格式:Excel (Sets + Books)

JSON (Sets + Books)

YAML (Sets + Books)

HTML (Sets)

TSV (Sets)

CSV (Sets)

示例

生成一个新数据集

headers = ('first_name', 'last_name')

data = [

('John', 'Adams'),

('George', 'Washington')

]

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

添加新行

>>> data.append(('Henry', 'Ford'))

添加新列

>>> data.append_col((90, 67, 83), header='age')

导出为JSON格式

>>> print data.json

[

{

"last_name": "Adams",

"age": 90,

"first_name": "John"

},

{

"last_name": "Ford",

"age": 83,

"first_name": "Henry"

}

]

其他导出格式示例:

>>> data = tablib.Dataset(*[('Kenneth', 'Reitz', 23), ('Bessie', 'Monke', 22)],

headers=['First Name', 'Last Name', 'Age'])

>>> data.json

[{"Last Name": "Reitz", "First Name": "Kenneth", "Age": 22}, {"Last Name": "Monke", "First Name": "Bessie", "Age": 21}]

>>> data.yaml

- {Age: 22, First Name: Kenneth, Last Name: Reitz}

- {Age: 21, First Name: Bessie, Last Name: Monke}

>>> data.xlsx

 类似资料: