Working with TSV in Python
Tab-separated values (TSV) file handling with automatic format detection and high-performance data operations.
Installation
Section titled “Installation”pip install fairspecGetting Started
Section titled “Getting Started”The TSV format is handled by the CSV plugin, which provides:
load_csv_table- Load TSV files into tablessave_csv_table- Save tables to TSV filesCsvPlugin- Plugin for framework integration
For example:
from fairspec import load_csv_table, Resource
table = load_csv_table(Resource(data="table.tsv"))# the column types will be automatically inferredBasic Usage
Section titled “Basic Usage”Loading TSV Files
Section titled “Loading TSV Files”from fairspec import load_csv_table, Resourcefrom fairspec_metadata import CsvFileDialect
# Load a simple TSV filetable = load_csv_table(Resource(data="data.tsv"))
# Load with explicit formattable = load_csv_table(Resource( data="data.tsv", fileDialect=CsvFileDialect(delimiter="\t", headerRows=[1]),))
# Load multiple TSV files (concatenated)table = load_csv_table(Resource(data=["part1.tsv", "part2.tsv", "part3.tsv"]))Saving TSV Files
Section titled “Saving TSV Files”from fairspec import save_csv_tablefrom fairspec_metadata import CsvFileDialect
# Save with default optionssave_csv_table(table, path="output.tsv", fileDialect=CsvFileDialect(delimiter="\t"))
# Save with line terminator optionsave_csv_table(table, path="output.tsv", fileDialect=CsvFileDialect( delimiter="\t", lineTerminator="\r\n",))Advanced Features
Section titled “Advanced Features”Multi-Header Row Processing
Section titled “Multi-Header Row Processing”from fairspec import load_csv_table, Resourcefrom fairspec_metadata import CsvFileDialect
# TSV with multiple header rowstable = load_csv_table(Resource( data="multi-header.tsv", fileDialect=CsvFileDialect( delimiter="\t", headerRows=[1, 2], headerJoin="_", ),))Comment Handling
Section titled “Comment Handling”from fairspec import load_csv_table, Resourcefrom fairspec_metadata import CsvFileDialect
# TSV with comment linestable = load_csv_table(Resource( data="with-comments.tsv", fileDialect=CsvFileDialect( delimiter="\t", commentPrefix="#", headerRows=[1], ),))
# Or specify specific comment row numberstable = load_csv_table(Resource( data="with-comments.tsv", fileDialect=CsvFileDialect( delimiter="\t", commentRows=[1, 2], headerRows=[3], ),))Remote File Loading
Section titled “Remote File Loading”from fairspec import load_csv_table, Resource
# Load from URLtable = load_csv_table(Resource(data="https://example.com/data.tsv"))
# Load multiple remote filestable = load_csv_table(Resource(data=[ "https://api.example.com/data-2023.tsv", "https://api.example.com/data-2024.tsv",]))