How to use CSV In Python
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:
Simple Ways to Read TSV Files in Python
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.