High-performance parser and generator for PostgreSQL-compatible tab-separated values (TSV)
Maintained. Last release 5 months ago, commits in 3 of the last 26 weeks. Weaker on commit activity and release cadence.
pip install tsv2pyParse and generate tab-separated values (TSV) data
Tab-separated values (TSV) is a simple and popular format for data storage, data transfer, exporting data from and importing data to relational databases. For example, PostgreSQL COPY moves data between PostgreSQL tables and standard file-system files or in-memory stores, and its text format (a text file with one line per table row) is a generic version of TSV. Meanwhile, packages like asyncpg help efficiently insert, update or query data in bulk with binary data transfer between Python and PostgreSQL.
This package offers a high-performance alternative to convert data between a TSV text file and Python objects. The parser can read a TSV record into a Python tuple consisting of built-in Python types, one for each field. The generator can produce a TSV record from a tuple.
Installation
Even though tsv2py contains native code, the package is already pre-built for several target architectures. In most cases, you can install directly from a binary wheel, selected automatically by pip:
python3 -m pip install tsv2py
If a binary wheel is not available for the target platform, pip will attempt to install tsv2py from the source distribution. This will build the package on the fly as part of the installation process, which requires a C compiler such as gcc or clang. The following commands install a C compiler and the Python development headers on AWS Linux:
sudo yum groupinstall -y "Development Tools"
sudo yum install -y python3-devel python3-pip
If you lack a C compiler or the Python development headers, you will get error messages similar to the following:
error: command 'gcc' failed: No such file or directory
lib/tsv_parser.c:2:10: fatal error: Python.h: No such file or directory
Quick start
from tsv.helper import Parser
# specify the column structure
parser = Parser(fields=(bytes, date, datetime, float, int, str, UUID, bool))
# read and parse an entire file
with open(tsv_path, "rb") as f:
py_records = parser.parse_file(f)
# read and parse a file line by line
with open(tsv_path, "rb") as f:
for line in f:
py_record = parser.parse_line(line)
TSV format
Text format is a simple tabular format in which each record (table row) occupies a single line.