python 實(shí)現(xiàn)批量xls文件轉(zhuǎn)csv文件的方法
引言:以前寫的一個(gè)批量xls轉(zhuǎn)csv的python簡單腳本,用的是python2.7
#coding=utf-8 import os import time import logging import xlrd import csv #xls文件存放路徑 INPUTPATH= u"D:\\lsssl\\桌面\\xls文件" #生成的csv文件存放路徑 OUTPATH = u"D:\\lsssl\桌面\\csv" class changeCenter: def __init__(self): pass def getvalue(self,filename): self.mData = [] xlsfile=xlrd.open_workbook(filename) table = xlsfile.sheet_by_index(0)#sheet1 rownum = table.nrows #行 colsnum = table.ncols #列 for i in range(0,rownum): row = [] for j in range(0,colsnum): value = table.cell_value(i,j) if not isinstance(value,float): value = value.encode('gbk')#非數(shù)字轉(zhuǎn)一下碼 row.append(value) self.mData.append(tuple(row)) def write(self, path, filename): if not os.path.exists(path): os.makedirs(path) csvfile = file("tmp","wb") writer = csv.writer(csvfile) writer.writerows(self.mData) csvfile.close() if os.path.exists(os.path.join(path,filename+".old")): os.remove(os.path.join(path,filename+".old")) if os.path.exists(os.path.join(path,filename)): os.rename(os.path.join(path,filename),os.path.join(path,filename+".old")) os.rename('tmp', os.path.join(path,filename)) logging.info("write file finish") print "write",filename," finish" def handleExcel(): files,dirs,root = readFilename(INPUTPATH) for fi in files: strstock = os.path.join(INPUTPATH,fi) if os.path.exists(strstock): st = changeCenter() st.getvalue(strstock) name = fi.replace(".xls","") st.write(OUTPATH, name+".csv") else: print strstock+" don't exist" #獲取某個(gè)路徑下的所有文件 def readFilename(file_dir): for root, dirs, files in os.walk(file_dir): return files,dirs,root if __name__ == '__main__': handleExcel()
以上這篇python 實(shí)現(xiàn)批量xls文件轉(zhuǎn)csv文件的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 對python中xlsx,csv以及json文件的相互轉(zhuǎn)化方法詳解
- python pands實(shí)現(xiàn)execl轉(zhuǎn)csv 并修改csv指定列的方法
- 對python csv模塊配置分隔符和引用符詳解
- python生成以及打開json、csv和txt文件的實(shí)例
- python pandas讀取csv后,獲取列標(biāo)簽的方法
- python 快速把超大txt文件轉(zhuǎn)存為csv的實(shí)例
- Python對CSV、Excel、txt、dat文件的處理
- Python實(shí)現(xiàn)的簡單讀寫csv文件操作示例
- Python簡單爬蟲導(dǎo)出CSV文件的實(shí)例講解
- python 獲取頁面表格數(shù)據(jù)存放到csv中的方法
相關(guān)文章
Python學(xué)習(xí)之.iloc與.loc的區(qū)別、聯(lián)系和用法
loc和iloc都是pandas工具中定位某一行的函數(shù),下面這篇文章主要給大家介紹了關(guān)于Python學(xué)習(xí)之.iloc與.loc的區(qū)別、聯(lián)系和用法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05深入理解Python內(nèi)置函數(shù)map filter reduce及與列表推導(dǎo)式對比
這篇文章主要為大家介紹了Python內(nèi)置函數(shù)map filter reduce及與列表推導(dǎo)式對比方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06詳解Python中類方法@classmethod的應(yīng)用技巧
在Python中,類方法(class method)是一種特殊的方法,可以在不創(chuàng)建類的實(shí)例的情況下調(diào)用,本文將詳細(xì)介紹類方法的概念、用法以及在實(shí)際開發(fā)中的應(yīng)用場景,希望對大家有所幫助2024-03-03Python實(shí)現(xiàn)Excel自動(dòng)分組合并單元格
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)Excel自動(dòng)分組合并單元格,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02pytorch中.to(device) 和.cuda()的區(qū)別說明
這篇文章主要介紹了pytorch中.to(device) 和.cuda()的區(qū)別說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05Python中輸入和輸出(打印)數(shù)據(jù)實(shí)例方法
在本篇文章里小編給大家整理的是關(guān)于Python中輸入和輸出(打?。?shù)據(jù)實(shí)例方法以及相關(guān)知識點(diǎn),有需要的朋友們參考下。2019-10-10對Django 轉(zhuǎn)發(fā)和重定向的實(shí)例詳解
今天小編就為大家分享一篇對Django 轉(zhuǎn)發(fā)和重定向的實(shí)例詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08