Open In App

Convert TSV to TXT in Python

In this article, we are going to see how to convert TSV files to text files in Python. 

Approach:

Syntax:



csv.reader(file_name, delimiter="\t")

Parameters:

Example 1:



File Used:




# importing library
import csv
 
# Open tsv and txt files(open txt file in write mode)
tsv_file = open("Student.tsv")
txt_file = open("StudentOutput.txt", "w")
 
# Read tsv file and use delimiter as \t. csv.reader
# function returns a iterator
# which is stored in read_csv
read_tsv = csv.reader(tsv_file, delimiter="\t")
 
# write data in txt file line by line
for row in read_tsv:
    joined_string = "\t".join(row)
    txt_file.writelines(joined_string+'\n')
 
# close files
txt_file.close()

Output:

Example 2: 

File Used:




# importing library
import csv
 
# Open tsv and txt files(open txt file in write mode)
tsv_file = open("Downloads/Student-1.tsv")
txt_file = open("Downloads/student2.txt", "w")
 
# Read tsv file and use delimiter as \t. csv.reader
# function returns a iterator
# which is stored in read_csv
read_tsv = csv.reader(tsv_file, delimiter="\t")
 
# write data in txt file line by line
for row in read_tsv:
    joined_string = "\t".join(row)
    txt_file.writelines(joined_string+'\n')
 
# close files
txt_file.close()

Output:


Article Tags :