156k views
3 votes
Write a function that takes the name of a file with a .csv extension (a comma-separated value file), and writes a new file that is equivalent (same name and same data) but with a .tsv extension (a tab-separated value file: like a CSV but with tabs instead of commas separating the elements of the file). Note: the character used to represent a tab is ‘\t’.

User TeaLeef
by
4.3k points

1 Answer

4 votes

Answer:

import pandas as pd

def convert_to_tsv( csv_filename ):

df = pd.read_csv("csv_file")

df.to_csv("csv_filename.tsv", sep='\t')

Step-by-step explanation:

The python program uses the pandas' module to read in the csv file in the function "convert_to_tsv". The file is saved as a tsv file with the pandas to_csv method and the " sep= '\t' " attribute.

User CarlM
by
5.0k points