10000 Merge pull request #212 from pyexcel/dev · pyexcel/pyexcel@bae7027 · GitHub
[go: up one dir, main page]

Skip to content

Commit bae7027

Browse files
authored
Merge pull request #212 from pyexcel/dev
release 0.6.2
2 parents 6b33a8c + ce7fb84 commit bae7027

30 files changed

+2230
-339
lines changed

.gitignore

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ parts/
2525
sdist/
2626
var/
2727
wheels/
28-
pip-wheel-metadata/
2928
share/python-wheels/
3029
*.egg-info/
3130
.installed.cfg
@@ -143,10 +142,6 @@ dmypy.json
143142
# Cython debug symbols
144143
cython_debug/
145144

146-
# static files generated from Django application using `collectstatic`
147-
media
148-
static
149-
150145
# VirtualEnv rules
151146
# Virtualenv
152147
# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/
@@ -431,6 +426,9 @@ tmtags
431426
!.vscode/extensions.json
432427
*.code-workspace
433428

429+
# Local History for Visual Studio Code
430+
.history/
431+
434432
# Xcode rules
435433
# Xcode
436434
#

.moban.d/docs/source/pyexcel-index.rst.jj2

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,127 @@ Installation
2323

2424
{%include "installatio D7AE n.rst.jj2" %}
2525

26+
Suppose you have the following data in a dictionary:
27+
28+
========= ====
29+
Name Age
30+
========= ====
31+
Adam 28
32+
Beatrice 29
33+
Ceri 30
34+
Dean 26
35+
========= ====
36+
37+
you can easily save it into an excel file using the following code:
38+
39+
.. code-block:: python
40+
41+
>>> import pyexcel
42+
>>> # make sure you had pyexcel-xls installed
43+
>>> a_list_of_dictionaries = [
44+
... {
45+
... "Name": 'Adam',
46+
... "Age": 28
47+
... },
48+
... {
49+
... "Name": 'Beatrice',
50+
... "Age": 29
51+
... },
52+
... {
53+
... "Name": 'Ceri',
54+
... "Age": 30
55+
... },
56+
... {
57+
... "Name": 'Dean',
58+
... "Age": 26
59+
... }
60+
... ]
61+
>>> pyexcel.save_as(records=a_list_of_dictionaries, dest_file_name="your_file.xls")
62+
63+
And here's how to obtain the records:
64+
65+
.. code-block:: python
66+
67+
>>> import pyexcel as p
68+
>>> records = p.iget_records(file_name="your_file.xls")
69+
>>> for record in records:
70+
... print("%s is aged at %d" % (record['Name'], record['Age']))
71+
Adam is aged at 28
72+
Beatrice is aged at 29
73+
Ceri is aged at 30
74+
Dean is aged at 26
75+
>>> p.free_resources()
76+
77+
78+
Custom data rendering:
79+
80+
.. code-block:: python
81+
82+
>>> # pip install pyexcel-text==0.2.7.1
83+
>>> import pyexcel as p
84+
>>> ccs_insight2 = p.Sheet()
85+
>>> ccs_insight2.name = "Worldwide Mobile Phone Shipments (Billions), 2017-2021"
86+
>>> ccs_insight2.ndjson = """
87+
... {"year": ["2017", "2018", "2019", "2020", "2021"]}
88+
... {"smart phones": [1.53, 1.64, 1.74, 1.82, 1.90]}
89+
... {"feature phones": [0.46, 0.38, 0.30, 0.23, 0.17]}
90+
... """.strip()
91+
>>> ccs_insight2
92+
pyexcel sheet:
93+
+----------------+------+------+------+------+------+
94+
| year | 2017 | 2018 | 2019 | 2020 | 2021 |
95+
+----------------+------+------+------+------+------+
96+
| smart phones | 1.53 | 1.64 | 1.74 | 1.82 | 1.9 |
97+
+----------------+------+------+------+------+------+
98+
| feature phones | 0.46 | 0.38 | 0.3 | 0.23 | 0.17 |
99+
+----------------+------+------+------+------+------+
100+
101+
102+
Advanced usage :fire:
103+
----------------------
104+
105+
If you are dealing with big data, please consider these usages:
106+
107+
.. code-block:: python
108+
109+
>>> def increase_everyones_age(generator):
110+
... for row in generator:
111+
... row['Age'] += 1
112+
... yield row
113+
>>> def duplicate_each_record(generator):
114+
... for row in generator:
115+
... yield row
116+
... yield row
117+
>>> records = p.iget_records(file_name="your_file.xls")
118+
>>> io=p.isave_as(records=duplicate_each_record(increase_everyones_age(records)),
119+
... dest_file_type='csv', dest_lineterminator='\n')
120+
>>> print(io.getvalue())
121+
Age,Name
122+
29,Adam
123+
29,Adam
124+
30,Beatrice
125+
30,Beatrice
126+
31,Ceri
127+
31,Ceri
128+
27,Dean
129+
27,Dean
130+
<BLANKLINE>
131+
132+
133+
Two advantages of above method:
134+
135+
#. Add as many wrapping functions as you want.
136+
#. Constant memory consumption
137+
138+
139+
.. testcode::
140+
:hide:
141+
142+
>>> import os
143+
>>> os.unlink("your_file.xls")
144+
145+
146+
26147
For individual excel file formats, please install them as you wish:
27148

28149
{%include "plugins-list.rst.jj2"%}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% set sphinx=True %}
2+
3+
{% include "one-liners.rst.jj2" %}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% set sphinx=True %}
2+
3+
{% include "two-liners.rst.jj2" %}

0 commit comments

Comments
 (0)
0