File: load-tabular.rst

package info (click to toggle)
python-cogent 2024.5.7a1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 74,600 kB
  • sloc: python: 92,479; makefile: 117; sh: 16
file content (49 lines) | stat: -rw-r--r-- 1,363 bytes parent folder | download
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
.. jupyter-execute::
    :hide-code:

    import set_working_directory

Loading tabular data
--------------------

How to load data from a tsv file
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

We can load a file containing tab seperated values with the ``load_tabular`` app, providing it with the appropriate separator, ``sep="\t"``. 

.. jupyter-execute::
    :raises:

    from cogent3 import get_app

    load_tsv_app = get_app("load_tabular", sep="\t")
    tsv_data = load_tsv_app("data/stats.tsv")
    tsv_data

How to load data from a csv file
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

We can load a file containing comma seperated values with the ``load_tabular`` app, providing it with the seperator, ``sep=","``. 

In the above example, ``data`` is a ``cogent3.util.Table`` object, if we write it to disk in csv format, we can re-use it for this example too. 

.. jupyter-execute::
    :hide-code:

    from tempfile import NamedTemporaryFile

    tmpdir = NamedTemporaryFile(dir=".")
    path_to_csv_file = tmpdir.name

.. tip:: If you are executing this code on your machine, replace ``path_to_csv_file`` with the path containing your csv file!

.. jupyter-execute::
    :raises:

    from cogent3 import get_app

    tsv_data.write(path_to_csv_file, sep=",")

    load_csv_app = get_app("load_tabular", sep=",")
    csv_data = load_csv_app(path_to_csv_file)
    csv_data