File: exporting_data.rst

package info (click to toggle)
python-dbfread 2.0.7-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 384 kB
  • sloc: python: 1,146; makefile: 140
file content (68 lines) | stat: -rw-r--r-- 1,513 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
.. highlight:: python

Moving data to SQL, CSV, Pandas etc.
====================================


CSV
---

This uses the standard library ``csv`` module:

.. literalinclude:: ../examples/print_csv.py

The output is::

    NAME,BIRTHDATE
    Alice,1987-03-01
    Bob,1980-11-12


Pandas Data Frames
------------------

.. literalinclude:: ../examples/pandas_dataframe.py

This will print::

        BIRTHDATE   NAME
    0  1987-03-01  Alice
    1  1980-11-12    Bob

The ``iter()`` is required. Without it Pandas will not realize that it
can iterate over the table.

Pandas will create a new list internally before converting the records
to data frames. This means they will all be loaded into memory. There
seems to be no way around this at the moment.


dataset (SQL)
-------------

The `dataset <https://dataset.readthedocs.io/>`_ package makes it easy
to move data to a modern database. Here's how you can insert the
``people`` table into an SQLite database:

.. literalinclude:: ../examples/using_dataset.py

(This also creates the schema.)


dbf2sqlite
----------

You can use the included example program ``dbf2sqlite`` to insert
tables into an SQLite database::

    dbf2sqlite -o example.sqlite table1.dbf table2.dbf

This will create one table for each DBF file. You can also omit the
``-o example.sqlite`` option to have the SQL printed directly to
stdout.

If you get character encoding errors you can pass ``--encoding`` to
override the encoding, for example::

    dbf2sqlite --encoding=latin1 ...