| 1 |
.. module:: petl.io |
| 2 |
.. _io_usage: |
| 3 |
|
| 4 |
Usage - reading/writing tables |
| 5 |
============================== |
| 6 |
|
| 7 |
`petl` uses simple python functions for providing a rows and columns abstraction |
| 8 |
for reading and writing data from files, databases, and other sources. |
| 9 |
|
| 10 |
The main features that `petl` was designed are: |
| 11 |
|
| 12 |
- Pure python implementation based on `streams <https://docs.python.org/3/library/io.html>`, |
| 13 |
`iterators <https://docs.python.org/3/library/stdtypes.html?highlight=iterator#iterator-types>` |
| 14 |
, and other python types. |
| 15 |
- Extensible approach, only requiring package dependencies when using their |
| 16 |
functionality. |
| 17 |
- Use a Dataframe/Table like paradigm similar of Pandas, R, and others |
| 18 |
- Lightweight alternative to develop and maintain compared to heavier, |
| 19 |
full-featured frameworks, like PySpark, PyArrow and other ETL tools. |
| 20 |
|
| 21 |
.. _io_overview: |
| 22 |
|
| 23 |
Brief Overview |
| 24 |
-------------- |
| 25 |
|
| 26 |
.. _io_extract: |
| 27 |
|
| 28 |
Extract (read) |
| 29 |
^^^^^^^^^^^^^^ |
| 30 |
|
| 31 |
The "from..." functions extract a table from a file-like source or |
| 32 |
database. For everything except :func:`petl.io.db.fromdb` the |
| 33 |
``source`` argument provides information about where to extract the |
| 34 |
underlying data from. If the ``source`` argument is ``None`` or a |
| 35 |
string it is interpreted as follows: |
| 36 |
|
| 37 |
* ``None`` - read from stdin |
| 38 |
* string starting with `http://`, `https://` or `ftp://` - read from URL |
| 39 |
* string ending with `.gz` or `.bgz` - read from file via gzip decompression |
| 40 |
* string ending with `.bz2` - read from file via bz2 decompression |
| 41 |
* any other string - read directly from file |
| 42 |
|
| 43 |
.. _io_extract_codec: |
| 44 |
|
| 45 |
Some helper classes are also available for reading from other types of |
| 46 |
file-like sources, e.g., reading data from a Zip file, a string or a |
| 47 |
subprocess, see the section on :ref:`io_helpers` below for more |
| 48 |
information. |
| 49 |
|
| 50 |
Be aware that loading data from stdin breaks the table container |
| 51 |
convention, because data can usually only be read once. If you are |
| 52 |
sure that data will only be read once in your script or interactive |
| 53 |
session then this may not be a problem, however note that some |
| 54 |
:mod:`petl` functions do access the underlying data source more than |
| 55 |
once and so will not work as expected with data from stdin. |
| 56 |
|
| 57 |
.. _io_load: |
| 58 |
|
| 59 |
Load (write) |
| 60 |
^^^^^^^^^^^^ |
| 61 |
|
| 62 |
The "to..." functions load data from a table into a file-like source |
| 63 |
or database. For functions that accept a ``source`` argument, if the |
| 64 |
``source`` argument is ``None`` or a string it is interpreted as |
| 65 |
follows: |
| 66 |
|
| 67 |
* ``None`` - write to stdout |
| 68 |
* string ending with `.gz` or `.bgz` - write to file via gzip decompression |
| 69 |
* string ending with `.bz2` - write to file via bz2 decompression |
| 70 |
* any other string - write directly to file |
| 71 |
|
| 72 |
.. _io_load_codec: |
| 73 |
|
| 74 |
Some helper classes are also available for writing to other types of |
| 75 |
file-like sources, e.g., writing to a Zip file or string buffer, see |
| 76 |
the section on :ref:`io_helpers` below for more information. |
| 77 |
|
| 78 |
.. _io_builtin_formats: |
| 79 |
|
| 80 |
Built-in File Formats |
| 81 |
--------------------- |
| 82 |
|
| 83 |
.. module:: petl.io.csv |
| 84 |
.. _io_csv: |
| 85 |
|
| 86 |
Python objects |
| 87 |
^^^^^^^^^^^^^^ |
| 88 |
|
| 89 |
.. autofunction:: petl.io.base.fromcolumns |
| 90 |
|
| 91 |
Delimited files |
| 92 |
^^^^^^^^^^^^^^^ |
| 93 |
|
| 94 |
.. autofunction:: petl.io.csv.fromcsv |
| 95 |
.. autofunction:: petl.io.csv.tocsv |
| 96 |
.. autofunction:: petl.io.csv.appendcsv |
| 97 |
.. autofunction:: petl.io.csv.teecsv |
| 98 |
.. autofunction:: petl.io.csv.fromtsv |
| 99 |
.. autofunction:: petl.io.csv.totsv |
| 100 |
.. autofunction:: petl.io.csv.appendtsv |
| 101 |
.. autofunction:: petl.io.csv.teetsv |
| 102 |
|
| 103 |
|
| 104 |
.. module:: petl.io.pickle |
| 105 |
.. _io_pickle: |
| 106 |
|
| 107 |
Pickle files |
| 108 |
^^^^^^^^^^^^ |
| 109 |
|
| 110 |
.. autofunction:: petl.io.pickle.frompickle |
| 111 |
.. autofunction:: petl.io.pickle.topickle |
| 112 |
.. autofunction:: petl.io.pickle.appendpickle |
| 113 |
.. autofunction:: petl.io.pickle.teepickle |
| 114 |
|
| 115 |
|
| 116 |
.. module:: petl.io.text |
| 117 |
.. _io_text: |
| 118 |
|
| 119 |
Text files |
| 120 |
^^^^^^^^^^ |
| 121 |
|
| 122 |
.. autofunction:: petl.io.text.fromtext |
| 123 |
.. autofunction:: petl.io.text.totext |
| 124 |
.. autofunction:: petl.io.text.appendtext |
| 125 |
.. autofunction:: petl.io.text.teetext |
| 126 |
|
| 127 |
|
| 128 |
.. module:: petl.io.xml |
| 129 |
.. _io_xml: |
| 130 |
|
| 131 |
XML files |
| 132 |
^^^^^^^^^ |
| 133 |
|
| 134 |
.. autofunction:: petl.io.xml.fromxml |
| 135 |
.. autofunction:: petl.io.xml.toxml |
| 136 |
|
| 137 |
|
| 138 |
.. module:: petl.io.html |
| 139 |
.. _io_html: |
| 140 |
|
| 141 |
HTML files |
| 142 |
^^^^^^^^^^ |
| 143 |
|
| 144 |
.. autofunction:: petl.io.html.tohtml |
| 145 |
.. autofunction:: petl.io.html.teehtml |
| 146 |
|
| 147 |
|
| 148 |
.. module:: petl.io.json |
| 149 |
.. _io_json: |
| 150 |
|
| 151 |
JSON files |
| 152 |
^^^^^^^^^^ |
| 153 |
|
| 154 |
.. autofunction:: petl.io.json.fromjson |
| 155 |
.. autofunction:: petl.io.json.fromdicts |
| 156 |
.. autofunction:: petl.io.json.tojson |
| 157 |
.. autofunction:: petl.io.json.tojsonarrays |
| 158 |
|
| 159 |
.. module:: petl.io.streams |
| 160 |
.. _io_helpers: |
| 161 |
|
| 162 |
Python I/O streams |
| 163 |
^^^^^^^^^^^^^^^^^^ |
| 164 |
|
| 165 |
The following classes are helpers for extract (``from...()``) and load |
| 166 |
(``to...()``) functions that use a file-like data source. |
| 167 |
|
| 168 |
An instance of any of the following classes can be used as the ``source`` |
| 169 |
argument to data extraction functions like :func:`petl.io.csv.fromcsv` etc., |
| 170 |
with the exception of :class:`petl.io.sources.StdoutSource` which is |
| 171 |
write-only. |
| 172 |
|
| 173 |
An instance of any of the following classes can also be used as the ``source`` |
| 174 |
argument to data loading functions like :func:`petl.io.csv.tocsv` etc., with the |
| 175 |
exception of :class:`petl.io.sources.StdinSource`, |
| 176 |
:class:`petl.io.sources.URLSource` and :class:`petl.io.sources.PopenSource` |
| 177 |
which are read-only. |
| 178 |
|
| 179 |
The behaviour of each source can usually be configured by passing arguments |
| 180 |
to the constructor, see the source code of the :mod:`petl.io.sources` module |
| 181 |
for full details. |
| 182 |
|
| 183 |
.. autoclass:: petl.io.sources.StdinSource |
| 184 |
.. autoclass:: petl.io.sources.StdoutSource |
| 185 |
.. autoclass:: petl.io.sources.MemorySource |
| 186 |
.. autoclass:: petl.io.sources.PopenSource |
| 187 |
|
| 188 |
.. module:: petl.io.register |
| 189 |
.. _io_register: |
| 190 |
|
| 191 |
Custom I/O streams |
| 192 |
^^^^^^^^^^^^^^^^^^ |
| 193 |
|
| 194 |
For creating custom helpers for :ref:`remote I/O <io_remotes>` or |
| 195 |
`compression` use the following functions: |
| 196 |
|
| 197 |
.. autofunction:: petl.io.sources.register_reader |
| 198 |
.. autofunction:: petl.io.sources.register_writer |
| 199 |
.. autofunction:: petl.io.sources.get_reader |
| 200 |
.. autofunction:: petl.io.sources.get_writer |
| 201 |
|
| 202 |
See the source code of the classes in :mod:`petl.io.sources` module for |
| 203 |
more details. |
| 204 |
|
| 205 |
.. _io_extended_formats: |
| 206 |
|
| 207 |
Supported File Formats |
| 208 |
---------------------- |
| 209 |
|
| 210 |
.. module:: petl.io.xls |
| 211 |
.. _io_xls: |
| 212 |
|
| 213 |
Excel .xls files (xlrd/xlwt) |
| 214 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 215 |
|
| 216 |
.. note:: |
| 217 |
|
| 218 |
The following functions require `xlrd |
| 219 |
<https://pypi.python.org/pypi/xlrd>`_ and `xlwt |
| 220 |
<https://pypi.python.org/pypi/xlwt-future>`_ to be installed, |
| 221 |
e.g.:: |
| 222 |
|
| 223 |
$ pip install xlrd xlwt-future |
| 224 |
|
| 225 |
.. autofunction:: petl.io.xls.fromxls |
| 226 |
.. autofunction:: petl.io.xls.toxls |
| 227 |
|
| 228 |
|
| 229 |
.. module:: petl.io.xlsx |
| 230 |
.. _io_xlsx: |
| 231 |
|
| 232 |
Excel .xlsx files (openpyxl) |
| 233 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 234 |
|
| 235 |
.. note:: |
| 236 |
|
| 237 |
The following functions require `openpyxl |
| 238 |
<https://bitbucket.org/ericgazoni/openpyxl/wiki/Home>`_ to be |
| 239 |
installed, e.g.:: |
| 240 |
|
| 241 |
$ pip install openpyxl |
| 242 |
|
| 243 |
.. autofunction:: petl.io.xlsx.fromxlsx |
| 244 |
.. autofunction:: petl.io.xlsx.toxlsx |
| 245 |
.. autofunction:: petl.io.xlsx.appendxlsx |
| 246 |
|
| 247 |
|
| 248 |
.. module:: petl.io.numpy |
| 249 |
.. _io_numpy: |
| 250 |
|
| 251 |
Arrays (NumPy) |
| 252 |
^^^^^^^^^^^^^^ |
| 253 |
|
| 254 |
.. note:: |
| 255 |
|
| 256 |
The following functions require `numpy <http://www.numpy.org/>`_ |
| 257 |
to be installed, e.g.:: |
| 258 |
|
| 259 |
$ pip install numpy |
| 260 |
|
| 261 |
.. autofunction:: petl.io.numpy.fromarray |
| 262 |
.. autofunction:: petl.io.numpy.toarray |
| 263 |
.. autofunction:: petl.io.numpy.torecarray |
| 264 |
.. autofunction:: petl.io.numpy.valuestoarray |
| 265 |
|
| 266 |
|
| 267 |
.. module:: petl.io.pandas |
| 268 |
.. _io_pandas: |
| 269 |
|
| 270 |
DataFrames (pandas) |
| 271 |
^^^^^^^^^^^^^^^^^^^ |
| 272 |
|
| 273 |
.. note:: |
| 274 |
|
| 275 |
The following functions require `pandas |
| 276 |
<http://pandas.pydata.org/>`_ to be installed, e.g.:: |
| 277 |
|
| 278 |
$ pip install pandas |
| 279 |
|
| 280 |
.. autofunction:: petl.io.pandas.fromdataframe |
| 281 |
.. autofunction:: petl.io.pandas.todataframe |
| 282 |
|
| 283 |
|
| 284 |
.. module:: petl.io.pytables |
| 285 |
.. _io_pytables: |
| 286 |
|
| 287 |
HDF5 files (PyTables) |
| 288 |
^^^^^^^^^^^^^^^^^^^^^ |
| 289 |
|
| 290 |
.. note:: |
| 291 |
|
| 292 |
The following functions require `PyTables |
| 293 |
<https://pytables.github.io/index.html>`_ to be installed, e.g.:: |
| 294 |
|
| 295 |
$ # install HDF5 |
| 296 |
$ apt-get install libhdf5-7 libhdf5-dev |
| 297 |
$ # install other prerequisites |
| 298 |
$ pip install cython |
| 299 |
$ pip install numpy |
| 300 |
$ pip install numexpr |
| 301 |
$ # install PyTables |
| 302 |
$ pip install tables |
| 303 |
|
| 304 |
.. autofunction:: petl.io.pytables.fromhdf5 |
| 305 |
.. autofunction:: petl.io.pytables.fromhdf5sorted |
| 306 |
.. autofunction:: petl.io.pytables.tohdf5 |
| 307 |
.. autofunction:: petl.io.pytables.appendhdf5 |
| 308 |
|
| 309 |
|
| 310 |
.. module:: petl.io.bcolz |
| 311 |
.. _io_bcolz: |
| 312 |
|
| 313 |
Bcolz ctables |
| 314 |
^^^^^^^^^^^^^ |
| 315 |
|
| 316 |
.. note:: |
| 317 |
|
| 318 |
The following functions require `bcolz <http://bcolz.blosc.org>`_ |
| 319 |
to be installed, e.g.:: |
| 320 |
|
| 321 |
$ pip install bcolz |
| 322 |
|
| 323 |
.. autofunction:: petl.io.bcolz.frombcolz |
| 324 |
.. autofunction:: petl.io.bcolz.tobcolz |
| 325 |
.. autofunction:: petl.io.bcolz.appendbcolz |
| 326 |
|
| 327 |
.. module:: petl.io.whoosh |
| 328 |
.. _io_whoosh: |
| 329 |
|
| 330 |
Text indexes (Whoosh) |
| 331 |
^^^^^^^^^^^^^^^^^^^^^ |
| 332 |
|
| 333 |
.. note:: |
| 334 |
|
| 335 |
The following functions require |
| 336 |
`Whoosh <https://pypi.python.org/pypi/Whoosh/>`_ |
| 337 |
to be installed, e.g.:: |
| 338 |
|
| 339 |
$ pip install whoosh |
| 340 |
|
| 341 |
.. autofunction:: petl.io.whoosh.fromtextindex |
| 342 |
.. autofunction:: petl.io.whoosh.searchtextindex |
| 343 |
.. autofunction:: petl.io.whoosh.searchtextindexpage |
| 344 |
.. autofunction:: petl.io.whoosh.totextindex |
| 345 |
.. autofunction:: petl.io.whoosh.appendtextindex |
| 346 |
|
| 347 |
.. module:: petl.io.avro |
| 348 |
.. _io_avro: |
| 349 |
|
| 350 |
Avro files (fastavro) |
| 351 |
^^^^^^^^^^^^^^^^^^^^^ |
| 352 |
|
| 353 |
.. note:: |
| 354 |
|
| 355 |
The following functions require `fastavro |
| 356 |
<https://github.com/fastavro/fastavro>`_ to be |
| 357 |
installed, e.g.:: |
| 358 |
|
| 359 |
$ pip install fastavro |
| 360 |
|
| 361 |
.. autofunction:: petl.io.avro.fromavro |
| 362 |
.. autofunction:: petl.io.avro.toavro |
| 363 |
.. autofunction:: petl.io.avro.appendavro |
| 364 |
|
| 365 |
.. literalinclude:: ../petl/test/io/test_avro_schemas.py |
| 366 |
:name: logical_schema |
| 367 |
:language: python |
| 368 |
:caption: Avro schema for logical types |
| 369 |
:start-after: begin_logical_schema |
| 370 |
:end-before: end_logical_schema |
| 371 |
|
| 372 |
.. literalinclude:: ../petl/test/io/test_avro_schemas.py |
| 373 |
:name: nullable_schema |
| 374 |
:language: python |
| 375 |
:caption: Avro schema with nullable fields |
| 376 |
:start-after: begin_nullable_schema |
| 377 |
:end-before: end_nullable_schema |
| 378 |
|
| 379 |
.. literalinclude:: ../petl/test/io/test_avro_schemas.py |
| 380 |
:name: array_schema |
| 381 |
:language: python |
| 382 |
:caption: Avro schema with array values in fields |
| 383 |
:start-after: begin_array_schema |
| 384 |
:end-before: end_array_schema |
| 385 |
|
| 386 |
.. literalinclude:: ../petl/test/io/test_avro_schemas.py |
| 387 |
:name: complex_schema |
| 388 |
:language: python |
| 389 |
:caption: Example of recursive complex Avro schema |
| 390 |
:start-after: begin_complex_schema |
| 391 |
:end-before: end_complex_schema |
| 392 |
|
| 393 |
.. module:: petl.io.gsheet |
| 394 |
.. _io_gsheet: |
| 395 |
|
| 396 |
Google Sheets (gspread) |
| 397 |
^^^^^^^^^^^^^^^^^^^^^^^ |
| 398 |
|
| 399 |
.. warning:: |
| 400 |
|
| 401 |
This is a experimental feature. API and behavior may change between releases |
| 402 |
with some possible breaking changes. |
| 403 |
|
| 404 |
.. note:: |
| 405 |
|
| 406 |
The following functions require `gspread |
| 407 |
<https://github.com/burnash/gspread>`_ to be installed, |
| 408 |
e.g.:: |
| 409 |
|
| 410 |
$ pip install gspread |
| 411 |
|
| 412 |
.. autofunction:: petl.io.gsheet.fromgsheet |
| 413 |
.. autofunction:: petl.io.gsheet.togsheet |
| 414 |
.. autofunction:: petl.io.gsheet.appendgsheet |
| 415 |
|
| 416 |
.. module:: petl.io.db |
| 417 |
.. _io_db: |
| 418 |
|
| 419 |
Databases |
| 420 |
--------- |
| 421 |
|
| 422 |
.. note:: |
| 423 |
|
| 424 |
For reading and writing to databases, the following functions require |
| 425 |
`SQLAlchemy <http://www.sqlalchemy.org/>` and the database specific driver |
| 426 |
to be installed along petl, e.g.:: |
| 427 |
|
| 428 |
$ pip install sqlalchemy |
| 429 |
$ pip install sqlite3 |
| 430 |
$ pip install pymysql |
| 431 |
|
| 432 |
.. autofunction:: petl.io.db.fromdb |
| 433 |
.. autofunction:: petl.io.db.todb |
| 434 |
.. autofunction:: petl.io.db.appenddb |
| 435 |
|
| 436 |
.. module:: petl.io.remote |
| 437 |
.. _io_remotes: |
| 438 |
|
| 439 |
Remote and Cloud Filesystems |
| 440 |
---------------------------- |
| 441 |
|
| 442 |
The following classes are helpers for reading (``from...()``) and writing |
| 443 |
(``to...()``) functions transparently as a file-like source. |
| 444 |
|
| 445 |
There are no need to instantiate them. They are used in the mecanism described |
| 446 |
in :ref:`Extract <io_extract>` and :ref:`Load <io_load>`. |
| 447 |
|
| 448 |
It's possible to read and write just by prefixing the protocol (e.g: `s3://`) |
| 449 |
in the source path of the file. |
| 450 |
|
| 451 |
.. note:: |
| 452 |
|
| 453 |
For reading and writing to remote filesystems, the following functions |
| 454 |
requires `fsspec <https://filesystem-spec.readthedocs.io/>` to be installed |
| 455 |
along petl package e.g.:: |
| 456 |
|
| 457 |
$ pip install fsspec |
| 458 |
|
| 459 |
The supported filesystems with their URI formats can be found in fsspec |
| 460 |
documentation: |
| 461 |
|
| 462 |
- `Built-in Implementations <https://filesystem-spec.readthedocs.io/en/latest/api.html#built-in-implementations>`__ |
| 463 |
- `Other Known Implementations <https://filesystem-spec.readthedocs.io/en/latest/api.html#other-known-implementations>`__ |
| 464 |
|
| 465 |
Remote sources |
| 466 |
^^^^^^^^^^^^^^ |
| 467 |
|
| 468 |
.. autoclass:: petl.io.remotes.RemoteSource |
| 469 |
.. autoclass:: petl.io.remotes.SMBSource |
| 470 |
|
| 471 |
.. _io_deprecated: |
| 472 |
|
| 473 |
Deprecated I/O sources |
| 474 |
^^^^^^^^^^^^^^^^^^^^^^ |
| 475 |
|
| 476 |
The following helpers are deprecated and will be removed in a future version. |
| 477 |
|
| 478 |
It's functionality was replaced by helpers in :ref:`Remote helpers <io_remotes>`. |
| 479 |
|
| 480 |
.. autoclass:: petl.io.sources.FileSource |
| 481 |
.. autoclass:: petl.io.sources.GzipSource |
| 482 |
.. autoclass:: petl.io.sources.BZ2Source |
| 483 |
.. autoclass:: petl.io.sources.ZipSource |
| 484 |
.. autoclass:: petl.io.sources.URLSource |