批量将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')
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容