Home > other >  Convert multiple files from tsv to csv using pandas and glob
Convert multiple files from tsv to csv using pandas and glob

Time:03-04

While trying to convert the tab separated files (.tsv extension) to comma separated files (.csv extension) from local folder, it converted first two files to .csv but the last 2 files are not converted to .csv.

Here is list of files format :

     dir--
          abc.tsv
          red 1.tsv
          yellow 2022.01.20.tsv
          blue 2022.01.28.tsv

for the first 2 files will be converting into .csv as successfully. But last 2 files are not being converted as .csv and it's throwing the error like :

ValueError: Excel file format cannot be determined, you must specify an engine manually.

Here is the sample code snippet :

import glob
import pandas as pd

tsvfiles = 
glob.glob('C:/Users/xxxx/xxx/xxx/*.tsv') 
for tsv_file in tsvfiles:
    out = tsv_file.split('.')[0] '.csv'
    df = pd.read_csv(tsv_file) 
    df.to_csv(out) 

CodePudding user response:

You can try this code:

import pandas as pd
import glob

path = '/home/studio-lab-user/sagemaker-studiolab-notebooks/tsv'
tsvfiles = glob.glob(path   "/*.tsv") 
for t in tsvfiles:
    tsv = pd.read_table(t, sep='\t')
    tsv.to_csv(t[:-4]   '.csv', index=False)

  • Related