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. Code:
    import pandas as pd


  2. Code:
    df = pd.read_csv('my_file.txt', sep='\t', header=None)


  3. Code:
    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
Code:
'\t'
between two values in a given column.

Input:
Code:
'my_file.tsv'

[Image: image-12.png]Figure: File
Code:
'my_file.tsv'
with tab
Code:
'\t'
separated values.


Code:
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:
Code:
'my_file.csv'

Code:
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
Code:
'\t'
with a comma
Code:
','
character using the
Code:
string.replace()
method. This works if two values are separated by exactly one tabular character.

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

[Image: image-13.png]

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

Code:
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
Code:
'my_file.csv'
:

[Image: image-14.png]

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

Method 2: Regex Replace Arbitrary Tabs

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

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

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

[Image: image-16.png]

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

Code:
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
Code:
'my_file.csv'
:

[Image: image-17.png]

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
Code:
pd.read_csv(filename, sep='\t+', header=None)
and then write the DataFrame to a file using
Code:
df.to_csv(outfilename, header=None)
.

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

[Image: image-16.png]

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

Code:
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
Code:
'my_file.csv'
:

[Image: image-18.png]

You can also use the simpler
Code:
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! [Image: 1f40d.png][Image: 1f49b.png]

Regex Humor

[Image: image-133.png][i]Wait, forgot to escape a space. Wheeeeee[taptaptap]eeeeee. (source)[/i]
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: