Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

data-formats: Add example Python snippets for TSV handling #239

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@
# these URLs block the client the linkchecker uses
r'^https://www\.pnas\.org/doi/10\.1073/pnas\.1507071112',
r'^https://www\.ncbi\.nlm\.nih\.gov/books/NBK25501',
# This URL returns 403 in GH Actions, but succeeds locally.
r'^https://wiki\.mozilla\.org/CA/Included_Certificates',
# we specifically use this as an example of a link that _won't_ work
r'^https://nextstrain\.org/ncov/gisaid/21L/global/6m/2024-01-10',
]
Expand Down
28 changes: 28 additions & 0 deletions src/reference/data-formats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,34 @@ When using `tsv-utils <https://opensource.ebay.com/tsv-utils/>`__
| tsv-uniq -H -f strain \
| csvtk fix-quotes --tabs > output.tsv

If you are writing Python scripts that process TSV files, we recommend using the
`csv module <https://docs.python.org/3/library/csv.html>`__ for file I/O.

.. note::

Be sure to follow `csv module's recommendation <https://docs.python.org/3/library/csv.html#id4>`__
to open files with ``newline=''``.

Reading a TSV file:

.. code-block:: Python

with open(input_file, 'r', newline='') as handle:
reader = csv.reader(handle, delimiter='\t')
for row in reader:
...

Writing a TSV file:

.. code-block:: Python

with open(output_file, 'w', newline='') as output_handle:
tsv_writer = csv.writer(output_handle, delimiter='\t')
tsv_writer.writerow(header)
for record in records:
tsv_writer.writerow(record)


See our internal `discussion on TSV standardization <https://github.com/nextstrain/augur/issues/1566>`__ for more details.

JSON
Expand Down
Loading