如下所示:
# -*- coding: utf-8 -*-import os import pandas as pdimport numpy as np#from os import sysdef appendStr(strs): return "BOQ" + strsdef addBOQ(dirs, csv_file): data = pd.read_csv(os.path.join(dirs, csv_file), encoding="gbk") data = data[data[u"BOQ條碼"] != 'None'] data[u"BOQ條碼"] = data[u"BOQ條碼"].astype(np.str) data[u"BOQ條碼"] = data[u"BOQ條碼"].apply(appendStr, 1) data.to_csv(os.path.join(dirs, csv_file), index=False, encoding="gbk")def execl2csvbypandas(dirs, excel_file, addStr): newdir = os.path.join(dirs, "csvdir") if not os.path.isdir(newdir): os.makedirs(newdir) filename = os.path.splitext(excel_file) data_xls = pd.read_excel(os.path.join(dirs, excel_file), 'Sheet1', index_col=0) csvname = os.path.join(newdir, filename[0].join(['sheet1', '.csv'])) data_xls.to_csv(csvname, encoding='gbk') if True == addStr: addBOQ(newdir, csvname)def procExeclFiles(arg, dirs, files): print arg for f in files: file_path = os.path.join(dirs, f) if os.path.isfile(file_path): print f execl2csvbypandas(dirs, f, arg) print "end...." if __name__ == "__main__": #遍歷目錄,并把改目錄下的execl轉為csv,然后存入該目錄下csvdir目錄下,傳參數True時,是給csv中BOQ條碼列添加BOQ字符串,一般為False os.path.walk(r"C:/Users/Desktop/test", procExeclFiles, (True))
以上這篇python pands實現execl轉csv 并修改csv指定列的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。
新聞熱點
疑難解答