Skip to content

Commit

Permalink
data-formats: Add example Python snippets for TSV handling
Browse files Browse the repository at this point in the history
Prompted by @jameshadfield's request
<#238 (comment)>

Snippets are simplified versions of TSV handling in Augur.
  • Loading branch information
joverlee521 committed Dec 4, 2024
1 parent 38d42fd commit 7e1b4b5
Showing 1 changed file with 28 additions and 0 deletions.
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 using custom Python scripts to handle TSV files, we recommend using the
`csv module <https://docs.python.org/3/library/csv.html>`__ to read and write the files.

.. note::

Be sure to follow `csv module's recommendation <https://docs.python.org/3/library/csv.html#id4>`__
to open files with the ``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_file(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

0 comments on commit 7e1b4b5

Please sign in to comment.