圆月山庄资源网 Design By www.vgjia.com

如下所示:

# -*- coding: utf-8 -*-
import os 
import pandas as pd
import numpy as np
#from os import sys

def appendStr(strs):
 return "BOQ" + strs

def 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指定列的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

标签:
python,pands,execl,csv

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com