批量将excel转换为csv 2019-02-19

import os
import pandas as pd

pathname = './data/1-27_analyze/1-16/features_marked'

file_list = [pathname + '/' + i for i in os.listdir(pathname)]

#there are only .xlsx and .csv in the directory

# tansfer .xlsx to .csv
for file in file_list:
    if(file[-1] == 'v'):
        pass
    else:  # only deal with .xlsx file
        file_csv_old = file[0:-5] + '.csv'
        if(os.path.exists(file_csv_old)):  # remomve the csvs that already exist
            os.remove(file_csv_old)
        file_excel = pd.read_excel(file)
        file_excel = file_excel.dropna() # to delete rows with missing values
        file_excel.to_csv(file[0:-5]+'.csv', header=False, index=False, encoding='utf-8')

# transfer .csv to .xlsx
for file in file_list:
    if(file[-1] == 'x'):
        pass
    else:
        file_excel_old = file[0:-4] + '.xlsx'
        if(os.path.exists(file_excel_old)):
            os.remove(file_excel_old)
        file_csv = pd.read_csv(file)
        file_csv = file_csv.dropna()
        file_csv.to_excel(file[0:-4] + '_new.xlsx', header = False, index = False, encoding = 'utf-8')

你可能感兴趣的:(批量将excel转换为csv 2019-02-19)