如下所示:
# -*- 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指定列的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
最近想下m3u8格式视频流但是网址太卡好慢看不了所以搞了个python脚本下载 ,给有需要的也用用 ,可以有点小问题大家可以改改,搬或者移到其他视频流下载上,不要嫌弃
PAT 甲级真题 1006 Sign In and Sign Out (25分) python实现
OpenCV计算机视觉实战(Python)| 03、阈值与平滑处理
网友评论