Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Convert Tab-Delimited File to CSV in Python?

#1
How to Convert Tab-Delimited File to CSV in Python?

5/5 – (1 vote)

The easiest way to convert a tab-delimited values (TSV) file to a comma-separated values (CSV) file is to use the following three lines of code:

  1. import pandas as pd
  2. df = pd.read_csv('my_file.txt', sep='\t', header=None)
  3. df.to_csv('my_file.csv', header=None)

We’ll explain this and other approaches in more detail next—scroll down to Method 3 for this exact method.

Problem Formulation


Given a tab-delimited file with one tab character '\t' between two values in a given column.

Input: 'my_file.tsv'

Figure: File 'my_file.tsv' with tab '\t' separated values.

Alice	DataScience	$100000
Bob Programmer $90000
Carl Manager $122000
Dave Freelancer $144000

How to convert the tab-delimited values (TSV) to a comma-separated values (CSV) file?

Output: 'my_file.csv'

0,Alice,DataScience,$100000
1,Bob,Programmer,$90000
2,Carl,Manager,$122000
3,Dave,Freelancer,$144000

We’ll also look at slight variations of this problem. Let’s go!

Method 1: String Replace Single Tab


The most straightforward way to convert a tab-delimited (TSV) to a comma-separated (CSV) file in Python is to replace each tabular character '\t' with a comma ',' character using the string.replace() method. This works if two values are separated by exactly one tabular character.

Here’s an example input file 'my_file.tsv':


Here’s an example of some code to convert the tab-delimited file to the CSV file:

with open('my_file.tsv') as f: # Read space-delimited file and replace all empty spaces by commas data = f.read().replace('\t', ',') # Write the CSV data in the output file print(data, file=open('my_file.csv', 'w'))

Output file 'my_file.csv':


If you have any doubts, feel free to dive into our related tutorials:

Method 2: Regex Replace Arbitrary Tabs


To replace one '\t' or more tabs '\t\t\t' between two column values with a comma ',' and obtain a CSV, use the regular expressions operation re.sub('[\t]+', ',', data) on the space-separated data.

If you have any doubts, feel free to dive into our related tutorials:

Here’s an example input file 'my_file.tsv', notice the additional tabular characters that may separate two column values:


Here’s an example of some code to convert the TSV to the CSV file:

import re with open('my_file.txt') as infile: # Read space-delimited file and replace all empty spaces by commas data = re.sub('[ ]+', ',', infile.read()) # Write the CSV data in the output file print(data, file=open('my_file.csv', 'w'))

Output file 'my_file.csv':


Method 3: Pandas read_csv() and to_csv()


To convert a tab-delimited file to a CSV, first read the file into a Pandas DataFrame using pd.read_csv(filename, sep='\t+', header=None) and then write the DataFrame to a file using df.to_csv(outfilename, header=None).

Here’s an example input file 'my_file.tsv':


Here’s an example of some code to convert the tab-delimited file to the CSV file:

import pandas as pd # Read space-delimited file
df = pd.read_csv('my_file.tsv', sep='\t+', header=None) # Write DataFrame to file
df.to_csv('my_file.csv', header=None)

Output file 'my_file.csv':


You can also use the simpler sep='\t' if you are sure that only a single tabular character separates two column values.

If you have any doubts, feel free to dive into our related tutorials:

Summary


We examined three great ways to convert a space-delimited to a comma-separated CSV file:

Thanks for taking the time to read this article, my friend! ??


Regex Humor


Wait, forgot to escape a space. Wheeeeee[taptaptap]eeeeee. (source)



https://www.sickgaming.net/blog/2022/08/...in-python/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How to Convert MIDI to MP3 in Python – A Quick Overview xSicKxBot 0 2,390 09-02-2023, 02:04 PM
Last Post: xSicKxBot
  [Tut] How to Convert an Octal Escape Sequence in Python – And Vice Versa? xSicKxBot 0 1,329 12-08-2022, 01:23 PM
Last Post: xSicKxBot
  [Tut] How to Convert Octal String to Integer in Python xSicKxBot 0 1,294 12-04-2022, 08:39 AM
Last Post: xSicKxBot
  [Tut] Python Convert Hex to Base64 xSicKxBot 0 1,276 11-30-2022, 09:32 PM
Last Post: xSicKxBot
  [Tut] How to Create and Run a Batch File That Runs a Python Script? xSicKxBot 0 1,239 11-09-2022, 09:53 PM
Last Post: xSicKxBot
  [Tut] Python Create JSON File xSicKxBot 0 1,249 11-03-2022, 01:09 PM
Last Post: xSicKxBot
  [Tut] How to Filter Data from an Excel File in Python with Pandas xSicKxBot 0 1,221 10-31-2022, 05:36 AM
Last Post: xSicKxBot
  [Tut] How to Return a File From a Function in Python? xSicKxBot 0 1,325 10-21-2022, 09:47 AM
Last Post: xSicKxBot
  [Tut] How to Convert Bool (True/False) to a String in Python? xSicKxBot 0 1,301 10-04-2022, 11:37 AM
Last Post: xSicKxBot
  [Tut] How to Delete a Line from a File in Python? xSicKxBot 0 1,210 09-24-2022, 10:31 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016