![]() |
|
[Tut] How to Convert Tab-Delimited File to CSV in Python? - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Convert Tab-Delimited File to CSV in Python? (/thread-99803.html) |
[Tut] How to Convert Tab-Delimited File to CSV in Python? - xSicKxBot - 08-14-2022 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 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'![]() 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'![]() If you have any doubts, feel free to dive into our related tutorials:
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:
Here’s an example input file Code: 'my_file.tsv'![]() 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'![]() 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'![]() 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'![]() 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:
Thanks for taking the time to read this article, my friend! ![]() ![]() Regex Humor [i]Wait, forgot to escape a space. Wheeeeee[taptaptap]eeeeee. (source)[/i]
|