Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Simple Ways to Read TSV Files in Python

  • Difficulty Level : Basic
  • Last Updated : 23 Nov, 2021

In this article, we will discuss how to read TSV files in Python.

Input Data:

We will be using the same input file in all various implementation methods to see the output. Below is the input file from which we will read data.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course



Method 1: Using  Pandas

 We will read data from TSV file using pandas read_csv(). Along with the TSV file, we also pass separator as ‘\t’ for the tab character because, for tsv files, the tab character will separate each field. 

Syntax :

data=pandas.read_csv('filename.tsv',sep='\t')

Example: Program Using Pandas

Python3




# Simple Way to Read TSV Files in Python using pandas
# importing pandas library
import pandas as pd
 
# Passing the TSV file to
# read_csv() function
# with tab separator
# This function will
# read data from file
interviews_df = pd.read_csv('GeekforGeeks.tsv', sep='\t')
 
# printing data
print(interviews_df)

Output:

Method 2: Using CSV

 We use csv.reader() to convert the TSV file object to csv.reader object. And then pass the delimiter as ‘\t’ to the csv.reader. The delimiter is used to indicate the character which will be separating each field.

Syntax:



with open("filename.tsv") as file:
    tsv_file = csv.reader(file, delimiter="\t")

Example: Program Using csv

Python3




# Simple Way to Read TSV Files in Python using csv
# importing csv library
import csv
 
# open .tsv file
with open("GeekforGeeks.tsv") as file:
       
    # Passing the TSV file to 
    # reader() function
    # with tab delimiter
    # This function will
    # read data from file
    tsv_file = csv.reader(file, delimiter="\t")
     
    # printing data line by line
    for line in tsv_file:
        print(line)

Output:

Method 3: Using split

The very simple way to read data from TSV File in Python is using split(). We can read a given TSV file and store its data into a list.

Syntax: 

with open("filename.tsv") as file:
  for line in file:
    l=line.split('\t')

Example: Program Using split() 

Python3




# Simple Way to Read TSV Files in Python using split
ans = []
 
# open .tsv file
with open("GeekforGeeks.tsv") as f:
   
  # Read data line by line
  for line in f:
     
    # split data by tab
    # store it in list
    l=line.split('\t')
     
    # append list to ans
    ans.append(l)
 
# print data line by line
for i in ans:
    print(i)

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :