python操作excel和txt文件
2019獨角獸企業重金招聘Python工程師標準>>>
#!/usr/bin/env python
#coding:gbk
import xlrd
import os
filelists = os.listdir("/root/tools/ex")
for i in filelists:
? ? if i.endswith(".xls"):
? ? ? ? bk = xlrd.open_workbook(i)
? ? ? ? sh = bk.sheet_by_name("Sheet1")
? ? ? ? nrows = sh.nrows
? ? ? ? ncols = sh.ncols
? ? ? ? row_list = []
? ? ? ? for i in range(2,nrows):
? ? ? ? ? ? row_data = sh.row_values(i)
? ? ? ? ? ? row_data = ",".join(row_data)
? ? ? ? ? ? row_list.append(row_data)
? ? ? ? for i in row_list:
? ? ? ? ? ? print i
? ? elif i.endswith(".txt"):
? ? ? ? fd = open(i)
? ? ? ? print fd.read()
? ? ? ? fd.close()
?
轉載于:https://my.oschina.net/luoyedao/blog/1528252
總結
以上是生活随笔為你收集整理的python操作excel和txt文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu 16.04下减小/释放/清
- 下一篇: skill alpha protocol