Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
How To Convert Comma-Delimited String to a List In Python?
Next article icon

How to Convert Tab-Delimited File to Csv in Python?

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given a tab-delimited file and we need to convert it into a CSV file in Python. In this article, we will see how we can convert tab-delimited files to CSV files in Python.

Convert Tab-Delimited Files to CSV in Python

Below are some of the ways to Convert Tab-Delimited files to CSV in Python:

  • Using CSV Module
  • Using Pandas Library
  • Using Regular Expressions

index.tsv

tsv

Convert Tab-Delimited File to CSV Using CSV Module

Python's built-in CSV module provides convenient functions for reading and writing CSV files. We can utilize this module to convert a TSV file to CSV by specifying the appropriate delimiter. Here's how:

Python3
import csv  def tsv_to_csv(tsv_file, csv_file):     with open(tsv_file, 'r', newline='', encoding='utf-8') as tsvfile:         tsvreader = csv.reader(tsvfile, delimiter='\t')          with open(csv_file, 'w', newline='', encoding='utf-8') as csvfile:             csvwriter = csv.writer(csvfile)             csvwriter.writerows(tsvreader)   # Example usage tsv_to_csv('index.tsv', 'index.csv') print('successfully converted') 

Output

successfully converted

index.csv

csv

Convert Tab-Delimited File to Csv Using Pandas Library

Pandas is a powerful library for data manipulation in Python, including reading and writing various file formats. We can leverage its read_csv and to_csv functions to handle TSV to CSV conversion easily:

Python3
import pandas as pd   def tsv_to_csv(tsv_file, csv_file):     df = pd.read_csv(tsv_file, delimiter='\t')     df.to_csv(csv_file, index=False)   # Example usage tsv_to_csv('index.tsv', 'index.csv') print('successfully converted') 

Output

successfully converted

index.csv

csv

Convert Tab-Delimited File to Csv Using Regular Expressions

Another approach is to use regular expressions (re) to split the TSV file into fields based on the tab delimiter. Then, we can write the parsed data into a CSV file:

Python3
import csv import re   def tsv_to_csv(tsv_file, csv_file):     with open(tsv_file, 'r', encoding='utf-8') as tsvfile:         tsv_content = tsvfile.readlines()      with open(csv_file, 'w', newline='', encoding='utf-8') as csvfile:         csvwriter = csv.writer(csvfile)         for line in tsv_content:             row = re.split(r'\t', line.strip())             csvwriter.writerow(row)   # Example usage tsv_to_csv('index.tsv', 'index.csv') print('successfully converted') 

Output

successfully converted

index.csv

csv

Conclusion

In conclusion, converting a tab-delimited file to CSV in Python is a straightforward process, thanks to the powerful tools provided by the pandas library. By reading the tab-delimited file into a DataFrame and utilizing the to_csv method, you can effortlessly transform data formats. This method not only simplifies the conversion process but also enables easy manipulation and analysis of data in a CSV format, making it compatible with a wide range of applications.


Next Article
How To Convert Comma-Delimited String to a List In Python?
author
coders3409
Improve
Article Tags :
  • Python
  • Python Programs
  • python-csv
Practice Tags :
  • python

Similar Reads

  • How To Convert Comma-Delimited String to a List In Python?
    In Python, converting a comma-separated string to a list can be done by using various methods. In this article, we will check various methods to convert a comma-delimited string to a list in Python. Using str.split()The most straightforward and efficient way to convert a comma-delimited string to a
    1 min read
  • Convert Dict of List to CSV - Python
    To convert a dictionary of lists to a CSV file in Python, we need to transform the dictionary's structure into a tabular format that is suitable for CSV output. A dictionary of lists typically consists of keys that represent column names and corresponding lists that represent column data.For example
    4 min read
  • How to convert tab-separated file into a dataframe using Python
    In this article, we will learn how to convert a TSV file into a data frame using Python and the Pandas library. A TSV (Tab-Separated Values) file is a plain text file where data is organized in rows and columns, with each column separated by a tab character. It is a type of delimiter-separated file,
    4 min read
  • Convert List to Delimiter Separated String - Python
    The task of converting a list to a delimiter-separated string in Python involves iterating through the list and joining its elements using a specified delimiter. For example, given a list a = [7, "Gfg", 8, "is", "best", 9] and a delimiter "*", the goal is to produce a single string where each elemen
    3 min read
  • How to Load a File into the Python Console
    Loading files into the Python console is a fundamental skill for any Python programmer, enabling the manipulation and analysis of diverse data formats. In this article, we'll explore how to load four common file types—text, JSON, CSV, and HTML—into the Python console. Whether you're dealing with raw
    4 min read
  • How To Create A Csv File Using Python
    CSV stands for comma-separated values, it is a type of text file where information is separated by commas (or any other delimiter), they are commonly used in databases and spreadsheets to store information in an organized manner. In this article, we will see how we can create a CSV file using Python
    3 min read
  • Print the Content of a Txt File in Python
    Python provides a straightforward way to read and print the contents of a .txt file. Whether you are a beginner or an experienced developer, understanding how to work with file operations in Python is essential. In this article, we will explore some simple code examples to help you print the content
    3 min read
  • Convert CSV to JSON using Python
    Converting CSV to JSON using Python involves reading the CSV file, converting each row into a dictionary and then saving the data as a JSON file. For example, a CSV file containing data like names, ages and cities can be easily transformed into a structured JSON array, where each record is represent
    2 min read
  • Fastest Way to Read Excel File in Python
    Reading Excel files is a common task in data analysis and processing. Python provides several libraries to handle Excel files, each with its advantages in terms of speed and ease of use. This article explores the fastest methods to read Excel files in Python. Using pandaspandas is a powerful and fle
    3 min read
  • Python program to read CSV without CSV module
    CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the c
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences