python reading in multi-column tsv file with row numbers

How about using the following native Python codes:

with open('tsvfilename') as f:
    lines = f.read().split('\n')[:-1]
    for i, line in enumerate(lines):
        if i == 0: # header
            column_names = line.split()
            # ...
        else:
            data = line.split();
            # ...

Depends on what you want to do with the data afterwards (and if the file is truly a tsv with a \t delimiter). If you just want it in a set of lists you can use the csv module like so:

import csv
with open("tsv.tsv") as tsvfile:
    tsvreader = csv.reader(tsvfile, delimiter="\t")
    for line in tsvreader:
        print line[1:]

However I'd also recommend the DataFrame module from pandas for anything outside of simple python operations. It can be used as such:

from pandas import DataFrame
df = DataFrame.read_csv("tsv.tsv", sep="\t")

DataFrames allow for high level manipulation of data sets such as adding columns, finding averages, etc..


Import Pandas library

import pandas as pd
data = pd.read_csv('/ABC/DEF/TSV.tsv', sep='\t')

df = DataFrame.from_csv("tsv.tsv", sep="\t") is deprecated since version 0.21.0

df = pd.read_csv("tsv.tsv", sep="\t") is the way to go