mirror of
https://github.com/kennethreitz/tablib.git
synced 2026-06-05 23:10:17 +00:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| edbb16ec97 | |||
| dec5cea722 | |||
| 38183938dc | |||
| 7f1db4023f | |||
| b09fface1b | |||
| 69edb9def3 | |||
| ec54918f4a | |||
| ab6633549f | |||
| 56005d8022 | |||
| 36fa7ef097 |
+1
-1
@@ -6,5 +6,5 @@ python:
|
||||
- 3.5
|
||||
- 3.6
|
||||
install:
|
||||
- python setup.py install
|
||||
- pip install -r requirements.txt
|
||||
script: python test_tablib.py
|
||||
|
||||
+6
-6
@@ -87,7 +87,7 @@ JSON!
|
||||
+++++
|
||||
::
|
||||
|
||||
>>> print(data.json)
|
||||
>>> print(data.export('json'))
|
||||
[
|
||||
{
|
||||
"last_name": "Adams",
|
||||
@@ -106,7 +106,7 @@ YAML!
|
||||
+++++
|
||||
::
|
||||
|
||||
>>> print(data.yaml)
|
||||
>>> print(data.export('yaml'))
|
||||
- {age: 90, first_name: John, last_name: Adams}
|
||||
- {age: 83, first_name: Henry, last_name: Ford}
|
||||
|
||||
@@ -114,7 +114,7 @@ CSV...
|
||||
++++++
|
||||
::
|
||||
|
||||
>>> print(data.csv)
|
||||
>>> print(data.export('csv'))
|
||||
first_name,last_name,age
|
||||
John,Adams,90
|
||||
Henry,Ford,83
|
||||
@@ -124,20 +124,20 @@ EXCEL!
|
||||
::
|
||||
|
||||
>>> with open('people.xls', 'wb') as f:
|
||||
... f.write(data.xls)
|
||||
... f.write(data.export('xls'))
|
||||
|
||||
DBF!
|
||||
++++
|
||||
::
|
||||
|
||||
>>> with open('people.dbf', 'wb') as f:
|
||||
... f.write(data.dbf)
|
||||
... f.write(data.export('dbf'))
|
||||
|
||||
Pandas DataFrame!
|
||||
+++++++++++++++++
|
||||
::
|
||||
|
||||
>>> print(data.df):
|
||||
>>> print(data.export('df')):
|
||||
first_name last_name age
|
||||
0 John Adams 90
|
||||
1 Henry Ford 83
|
||||
|
||||
+4
-4
@@ -31,17 +31,17 @@ Tablib is an :ref:`MIT Licensed <mit>` format-agnostic tabular dataset library,
|
||||
... data.append(i)
|
||||
|
||||
|
||||
>>> print(data.json)
|
||||
>>> print(data.export('json'))
|
||||
[{"Last Name": "Reitz", "First Name": "Kenneth", "Age": 22}, {"Last Name": "Monke", "First Name": "Bessie", "Age": 21}]
|
||||
|
||||
>>> print(data.yaml)
|
||||
>>> print(data.export('yaml'))
|
||||
- {Age: 22, First Name: Kenneth, Last Name: Reitz}
|
||||
- {Age: 21, First Name: Bessie, Last Name: Monke}
|
||||
|
||||
>>> data.xlsx
|
||||
>>> data.export('xlsx')
|
||||
<censored binary data>
|
||||
|
||||
>>> data.df
|
||||
>>> data.export('df')
|
||||
First Name Last Name Age
|
||||
0 Kenneth Reitz 22
|
||||
1 Bessie Monke 21
|
||||
|
||||
+1
-2
@@ -49,7 +49,7 @@ Tablib is released under terms of `The MIT License`_.
|
||||
Tablib License
|
||||
--------------
|
||||
|
||||
Copyright 2016 Kenneth Reitz
|
||||
Copyright 2017 Kenneth Reitz
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -77,7 +77,6 @@ Pythons Supported
|
||||
|
||||
At this time, the following Python platforms are officially supported:
|
||||
|
||||
* cPython 2.6
|
||||
* cPython 2.7
|
||||
* cPython 3.3
|
||||
* cPython 3.4
|
||||
|
||||
+15
-7
@@ -115,30 +115,38 @@ Tablib's killer feature is the ability to export your :class:`Dataset` objects i
|
||||
|
||||
**Comma-Separated Values** ::
|
||||
|
||||
>>> data.csv
|
||||
>>> data.export('csv')
|
||||
Last Name,First Name,Age
|
||||
Reitz,Kenneth,22
|
||||
Monke,Bessie,20
|
||||
|
||||
**JavaScript Object Notation** ::
|
||||
|
||||
>>> data.json
|
||||
>>> data.export('json')
|
||||
[{"Last Name": "Reitz", "First Name": "Kenneth", "Age": 22}, {"Last Name": "Monke", "First Name": "Bessie", "Age": 20}]
|
||||
|
||||
|
||||
**YAML Ain't Markup Language** ::
|
||||
|
||||
>>> data.yaml
|
||||
>>> data.export('yaml')
|
||||
- {Age: 22, First Name: Kenneth, Last Name: Reitz}
|
||||
- {Age: 20, First Name: Bessie, Last Name: Monke}
|
||||
|
||||
|
||||
**Microsoft Excel** ::
|
||||
|
||||
>>> data.xls
|
||||
>>> data.export('xls')
|
||||
<censored binary data>
|
||||
|
||||
|
||||
**Pandas DataFrame** ::
|
||||
|
||||
>>> data.export('df')
|
||||
First Name Last Name Age
|
||||
0 Kenneth Reitz 22
|
||||
1 Bessie Monke 21
|
||||
|
||||
|
||||
------------------------
|
||||
Selecting Rows & Columns
|
||||
------------------------
|
||||
@@ -216,7 +224,7 @@ Let's add a dynamic column to our :class:`Dataset` object. In this example, we h
|
||||
|
||||
Let's have a look at our data. ::
|
||||
|
||||
>>> data.yaml
|
||||
>>> data.export('yaml')
|
||||
- {Age: 22, First Name: Kenneth, Grade: 0.6, Last Name: Reitz}
|
||||
- {Age: 20, First Name: Bessie, Grade: 0.75, Last Name: Monke}
|
||||
|
||||
@@ -246,7 +254,7 @@ For example, we can use the data available in the row to guess the gender of a s
|
||||
|
||||
Adding this function to our dataset as a dynamic column would result in: ::
|
||||
|
||||
>>> data.yaml
|
||||
>>> data.export('yaml')
|
||||
- {Age: 22, First Name: Kenneth, Gender: Male, Last Name: Reitz}
|
||||
- {Age: 20, First Name: Bessie, Gender: Female, Last Name: Monke}
|
||||
|
||||
@@ -346,7 +354,7 @@ When, it's often useful to create a blank row containing information on the upco
|
||||
|
||||
# Write spreadsheet to disk
|
||||
with open('grades.xls', 'wb') as f:
|
||||
f.write(tests.xls)
|
||||
f.write(tests.export('xls'))
|
||||
|
||||
The resulting **tests.xls** will have the following layout:
|
||||
|
||||
|
||||
@@ -48,7 +48,6 @@ install = [
|
||||
'xlrd',
|
||||
'xlwt',
|
||||
'pyyaml',
|
||||
'pandas'
|
||||
]
|
||||
|
||||
|
||||
@@ -81,4 +80,7 @@ setup(
|
||||
],
|
||||
tests_require=['pytest'],
|
||||
install_requires=install,
|
||||
extras_require={
|
||||
'pandas': ['pandas'],
|
||||
},
|
||||
)
|
||||
|
||||
+2
-2
@@ -18,8 +18,8 @@ from tablib.compat import OrderedDict, unicode
|
||||
|
||||
|
||||
__title__ = 'tablib'
|
||||
__version__ = '0.12.0'
|
||||
__build__ = 0x001200
|
||||
__version__ = '0.12.1'
|
||||
__build__ = 0x001201
|
||||
__author__ = 'Kenneth Reitz'
|
||||
__license__ = 'MIT'
|
||||
__copyright__ = 'Copyright 2017 Kenneth Reitz'
|
||||
|
||||
+10
-1
@@ -10,7 +10,10 @@ if sys.version_info[0] > 2:
|
||||
else:
|
||||
from cStringIO import StringIO as BytesIO
|
||||
|
||||
from pandas import DataFrame
|
||||
try:
|
||||
from pandas import DataFrame
|
||||
except ImportError:
|
||||
DataFrame = None
|
||||
|
||||
import tablib
|
||||
|
||||
@@ -21,6 +24,8 @@ extensions = ('df', )
|
||||
|
||||
def detect(stream):
|
||||
"""Returns True if given stream is a DataFrame."""
|
||||
if DataFrame is None:
|
||||
return False
|
||||
try:
|
||||
DataFrame(stream)
|
||||
return True
|
||||
@@ -30,6 +35,10 @@ def detect(stream):
|
||||
|
||||
def export_set(dset, index=None):
|
||||
"""Returns DataFrame representation of DataBook."""
|
||||
if DataFrame is None:
|
||||
raise NotImplementedError(
|
||||
'DataFrame Format requires `pandas` to be installed.'
|
||||
' Try `pip install tablib[pandas]`.')
|
||||
dataframe = DataFrame(dset.dict, columns=dset.headers)
|
||||
return dataframe
|
||||
|
||||
|
||||
Reference in New Issue
Block a user