美文网首页
批量将excel转换为csv 2019-02-19

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

作者: 默写年华Antifragile | 来源:发表于2019-02-19 22:44 被阅读0次
    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

          本文链接:https://www.haomeiwen.com/subject/wjuqyqtx.html