08-14-2022, 01:12 AM
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:
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 between two values in a given column.
Input:
Figure: File with tab separated values.
How to convert the tab-delimited values (TSV) to a comma-separated values (CSV) file?
Output:
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 with a comma character using the method. This works if two values are separated by exactly one tabular character.
Here’s an example input file:
![[Image: image-13.png]](https://blog.finxter.com/wp-content/uploads/2022/08/image-13.png)
Here’s an example of some code to convert the tab-delimited file to the CSV file:
Output file:
![[Image: image-14.png]](https://blog.finxter.com/wp-content/uploads/2022/08/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 or more tabs between two column values with a comma and obtain a CSV, use the regular expressions operation on the space-separated .
If you have any doubts, feel free to dive into our related tutorials:
Here’s an example input file, notice the additional tabular characters that may separate two column values:
![[Image: image-16.png]](https://blog.finxter.com/wp-content/uploads/2022/08/image-16.png)
Here’s an example of some code to convert the TSV to the CSV file:
Output file:
![[Image: image-17.png]](https://blog.finxter.com/wp-content/uploads/2022/08/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 and then write the DataFrame to a file using .
Here’s an example input file:
![[Image: image-16.png]](https://blog.finxter.com/wp-content/uploads/2022/08/image-16.png)
Here’s an example of some code to convert the tab-delimited file to the CSV file:
Output file:
![[Image: image-18.png]](https://blog.finxter.com/wp-content/uploads/2022/08/image-18.png)
You can also use the simpler 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]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f40d.png)
![[Image: 1f49b.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f49b.png)
Regex Humor
[i]Wait, forgot to escape a space. Wheeeeee[taptaptap]eeeeee. (source)[/i]
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:
- Code:
import pandas as pd
- Code:
df = pd.read_csv('my_file.txt', sep='\t', header=None)
- 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'Input:
Code:
'my_file.tsv'
Figure: File Code:
'my_file.tsv'Code:
'\t'Code:
Alice DataScience $100000
Bob Programmer $90000
Carl Manager $122000
Dave Freelancer $144000How 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,$144000We’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'Code:
','Code:
string.replace()Here’s an example input file
Code:
'my_file.tsv'![[Image: image-13.png]](https://blog.finxter.com/wp-content/uploads/2022/08/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]](https://blog.finxter.com/wp-content/uploads/2022/08/image-14.png)
If you have any doubts, feel free to dive into our related tutorials:
- Python String Replace Method
- Python FunctionCode:
open()
- Python to FileCode:
print()
- Python FunctionCode:
print()
- Python Context Managers
Method 2: Regex Replace Arbitrary Tabs
To replace one
Code:
'\t'Code:
'\t\t\t'Code:
','Code:
re.sub('[\t]+', ',', data)Code:
dataIf you have any doubts, feel free to dive into our related tutorials:
- Python Regex Code:
re.sub()
- Python Regex Character ClassCode:
[]
- Python Regex QuantifierCode:
+
- Python Regex Superpower
Here’s an example input file
Code:
'my_file.tsv'![[Image: image-16.png]](https://blog.finxter.com/wp-content/uploads/2022/08/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]](https://blog.finxter.com/wp-content/uploads/2022/08/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)Code:
df.to_csv(outfilename, header=None)Here’s an example input file
Code:
'my_file.tsv'![[Image: image-16.png]](https://blog.finxter.com/wp-content/uploads/2022/08/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]](https://blog.finxter.com/wp-content/uploads/2022/08/image-18.png)
You can also use the simpler
Code:
sep='\t'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:
- Method 1: String Replace Single Tab
- Method 2: Regex Replace Arbitrary Tabs
- Method 3: Pandas andCode:
read_csv()Code:to_csv()
Thanks for taking the time to read this article, my friend!
![[Image: 1f40d.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f40d.png)
![[Image: 1f49b.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f49b.png)
Regex Humor
[i]Wait, forgot to escape a space. Wheeeeee[taptaptap]eeeeee. (source)[/i]

