Skip to content

Commit

Permalink
read and write csv book to memory
Browse files Browse the repository at this point in the history
  • Loading branch information
chfw committed May 11, 2015
1 parent a268ae8 commit f00a506
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 71 deletions.
154 changes: 154 additions & 0 deletions doc/source/extendedcsv.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
Saving multiple sheets as CSV format
================================================================================

.. testcode::
:hide:

>>> import sys
>>> if sys.version_info[0] < 3:
... from StringIO import StringIO
... else:
... from io import BytesIO as StringIO
>>> from pyexcel_io import OrderedDict


Write to multiple sibling csv files
**********************

Here's the sample code to write a dictionary to multiple sibling csv files::

>>> from pyexcel_io import save_data
>>> data = OrderedDict() # from collections import OrderedDict
>>> data.update({"Sheet 1": [[1, 2, 3], [4, 5, 6]]})
>>> data.update({"Sheet 2": [["row 1", "row 2", "row 3"]]})
>>> save_data("your_file.csv", data)


Read from multiple sibling csv files
**********************

Here's the sample code::

>>> from pyexcel_io import get_data
>>> data = get_data("your_file.csv")
>>> import json
>>> print(json.dumps(data))
{"Sheet 1": [["1", "2", "3"], ["4", "5", "6"]], "Sheet 2": [["row 1", "row 2", "row 3"]]}

Here is what you would get::

>>> import glob
>>> list = glob.glob("your_file__*.csv")
>>> json.dumps(list)
'["your_file__Sheet 1__0.csv", "your_file__Sheet 2__1.csv"]'

Write a csv to memory
**********************

Here's the sample code to write a dictionary as a csv into memory::

>>> from pyexcel_io import save_data
>>> data = OrderedDict()
>>> data.update({"Sheet 1": [[1, 2, 3], [4, 5, 6]]})
>>> data.update({"Sheet 2": [[7, 8, 9], [10, 11, 12]]})
>>> io = StringIO()
>>> save_data(io, data, 'csv')
>>> # do something with the io
>>> # In reality, you might give it to your http response
>>> # object for downloading

Read from a csv from memory
*****************************

Continue from previous example::

>>> # This is just an illustration
>>> # In reality, you might deal with csv file upload
>>> # where you will read from requests.FILES['YOUR_XL_FILE']
>>> data = get_data(io)
>>> print(json.dumps(data))
{"Sheet 1": [["1", "2", "3"], ["4", "5", "6"]], "Sheet 2": [["7", "8", "9"], ["10", "11", "12"]]}


As a pyexcel plugin
--------------------

Import it in your file to enable this plugin::

from pyexcel.ext import io

Please note only pyexcel version 0.0.4+ support this.

Reading from multiple sibling csv files
************************

Here is the sample code::

>>> import pyexcel as pe
>>> from pyexcel.ext import io
>>> book = pe.get_book(file_name="your_file.csv")
>>> book
Sheet Name: Sheet 1
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 4 | 5 | 6 |
+---+---+---+
Sheet Name: Sheet 2
+-------+-------+-------+
| row 1 | row 2 | row 3 |
+-------+-------+-------+

Writing to multiple sibling csv files
**********************

Here is the sample code::

>>> book.save_as("another_file.csv")


Writing to a StringIO instance
================================

You need to pass a StringIO instance to Writer::

>>> io = StringIO()
>>> book.save_to_memory("csv", io)
>>> # then do something with io
>>> # In reality, you might give it to your http response
>>> # object for downloading


Reading from a IO instance
================================

You got to wrap the binary content with stream to get csv working::

>>> # This is just an illustration
>>> # In reality, you might deal with csv file upload
>>> # where you will read from requests.FILES['YOUR_CSV_FILE']
>>> memory_book = pe.get_book(file_type="csv", file_stream=io)
>>> memory_book
Sheet Name: Sheet 1
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 4 | 5 | 6 |
+---+---+---+
Sheet Name: Sheet 2
+-------+-------+-------+
| row 1 | row 2 | row 3 |
+-------+-------+-------+


.. testcode::
:hide:

>>> import os
>>> os.unlink("your_file__Sheet 1__0.csv")
>>> os.unlink("your_file__Sheet 2__1.csv")
>>> os.unlink("another_file__Sheet 1__0.csv")
>>> os.unlink("another_file__Sheet 2__1.csv")

95 changes: 39 additions & 56 deletions doc/source/plaincsv.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,33 @@ Write to a csv file
>>> from pyexcel_io import OrderedDict


Here's the sample code to write a dictionary to a csv file::
Here's the sample code to write an array to a csv file ::

>>> from pyexcel_io import store_data
>>> data = OrderedDict() # from collections import OrderedDict
>>> data.update({"Sheet 1": [[1, 2, 3], [4, 5, 6]]})
>>> data.update({"Sheet 2": [["row 1", "row 2", "row 3"]]})
>>> store_data("your_file.csv", data)
>>> from pyexcel_io import save_data
>>> data = [[1, 2, 3], [4, 5, 6]]
>>> save_data("your_file.csv", data)

Read from a csv file
**********************

Here's the sample code::

>>> from pyexcel_io import load_data
>>> data = load_data("your_file.csv")
>>> from pyexcel_io import get_data
>>> data = get_data("your_file.csv")
>>> import json
>>> print(json.dumps(data))
{"Sheet 1": [["1", "2", "3"], ["4", "5", "6"]], "Sheet 2": [["row 1", "row 2", "row 3"]]}
[["1", "2", "3"], ["4", "5", "6"]]

Write a csv to memory
**********************

Here's the sample code to write a dictionary as a csv into memory::

>>> from pyexcel_io import store_data
>>> data = OrderedDict()
>>> data.update({"Sheet 1": [[1, 2, 3], [4, 5, 6]]})
>>> data.update({"Sheet 2": [[7, 8, 9], [10, 11, 12]]})
>>> from pyexcel_io import save_data
>>> data = [[1, 2, 3], [4, 5, 6]]
>>> io = StringIO()
>>> store_data(io, data)
>>> save_data(io, data)
>>> # do something with the io
>>> # In reality, you might give it to your http response
>>> # object for downloading
Expand All @@ -61,47 +58,49 @@ Continue from previous example::
>>> # This is just an illustration
>>> # In reality, you might deal with csv file upload
>>> # where you will read from requests.FILES['YOUR_XL_FILE']
>>> data = load_data(io)
>>> data = get_data(io)
>>> print(json.dumps(data))
{"Sheet 1": [[1, 2, 3], [4, 5, 6]], "Sheet 2": [[7, 8, 9], [10, 11, 12]]}
[["1", "2", "3"], ["4", "5", "6"]]


As a pyexcel plugin
--------------------

Import it in your file to enable this plugin::

from pyexcel.ext import io

Please note only pyexcel version 0.0.4+ support this.

Reading from a csv file
************************
Reading from multiple csv file
**************************************

Here is the sample code::

>>> import pyexcel as pe
>>> from pyexcel.ext import io
>>> sheet = pe.get_book(file_name="your_file.csv")
>>> sheet = pe.get_sheet(file_name="your_file.csv")
>>> sheet
Sheet Name: Sheet 1
Sheet Name: your_file.csv
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 4 | 5 | 6 |
+---+---+---+
Sheet Name: Sheet 2
+-------+-------+-------+
| row 1 | row 2 | row 3 |
+-------+-------+-------+

Writing to a csv file
Writing to multiple sibling csv files
**********************

Here is the sample code::

>>> sheet.save_as("another_file.csv")


Writing to a StringIO instance
================================

You need to pass a StringIO instance to Writer::

>>> io = StringIO()
>>> sheet.save_to_memory("csv", io)
>>> # then do something with io
>>> # In reality, you might give it to your http response
>>> # object for downloading


Reading from a IO instance
================================

Expand All @@ -110,36 +109,20 @@ You got to wrap the binary content with stream to get csv working::
>>> # This is just an illustration
>>> # In reality, you might deal with csv file upload
>>> # where you will read from requests.FILES['YOUR_CSV_FILE']
>>> csvfile = "another_file.csv"
>>> with open(csvfile, "rb") as f:
... content = f.read()
... r = pe.get_book(file_type="csv", file_content=content)
... print(r)
...
Sheet Name: Sheet 1
>>> memory_sheet = pe.get_book(file_type="csv", file_stream=io)
>>> memory_sheet
Sheet Name: csv
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 4 | 5 | 6 |
+---+---+---+
Sheet Name: Sheet 2
+-------+-------+-------+
| row 1 | row 2 | row 3 |
+-------+-------+-------+


Writing to a StringIO instance
================================

You need to pass a StringIO instance to Writer::
.. testcode::
:hide:

>>> import os
>>> os.unlink("your_file.csv")

>>> data = [
... [1, 2, 3],
... [4, 5, 6]
... ]
>>> io = StringIO()
>>> sheet = pe.Sheet(data)
>>> sheet.save_to_memory("csv", io)
>>> # then do something with io
>>> # In reality, you might give it to your http response
>>> # object for downloading
41 changes: 28 additions & 13 deletions pyexcel_io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,7 @@ def load_data(filename,
elif is_string(type(filename)):
extension = filename.split(".")[-1]
else:
extension = FILE_FORMAT_CSV
if not is_string(type(filename)) and not isstream(filename):
# Not accepting non string file name
raise IOError(MESSAGE_ERROR_03)
raise IOError(MESSAGE_ERROR_03)
if extension in READERS:
book_class = READERS[extension]
if from_memory:
Expand Down Expand Up @@ -214,10 +211,7 @@ def get_writer(filename, file_type=None, **keywords):
elif is_string(type(filename)):
extension = filename.split(".")[-1]
else:
extension = FILE_FORMAT_CSV
if not is_string(type(filename)) and not isstream(filename):
# Not accepting non string file name
raise IOError(MESSAGE_ERROR_03)
raise IOError(MESSAGE_ERROR_03)
if extension in WRITERS:
writer_class = WRITERS[extension]
writer = writer_class(filename, **keywords)
Expand All @@ -239,10 +233,31 @@ def get_io(file_type):
return None


def store_data(afile, data, file_type=None, **keywords):
if is_string(type(afile)):
writer = get_writer(afile, **keywords)
def save_data(afile, data, file_type=None, **keywords):
to_store = data
if isinstance(data, list):
single_sheet_in_book = True
to_store = {"csv": data}
else:
writer = get_writer(afile, file_type, **keywords)
writer.write(data)
single_sheet_in_book = False

if isstream(afile):
file_type = FILE_FORMAT_CSV

writer = get_writer(
afile,
file_type=file_type,
single_sheet_in_book=single_sheet_in_book,
**keywords)
writer.write(to_store)
writer.close()


def get_data(afile, file_type=None, **keywords):
if isstream(afile) and file_type is None:
file_type='csv'
data = load_data(afile, file_type=file_type, **keywords)
if len(list(data.keys())) == 1:
return list(data.values())[0]
else:
return data

0 comments on commit f00a506

Please sign in to comment.