python作为脚本性语言,加上它的简便易用性。会经常当作脚本用来处理一下数据和格式。其中处理文件就是频繁用处之一。简单编写几个常用的xls和txt读写函数,以后可以快速复用。
#! /usr/bin/python
# coding:utf-8
import json
import xlrd
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
class ObjectFileReadAndWrite(object):
@classmethod
def readXlsToDict(cls, xlsFile):
'''
读取xls文件生成dict
'''
data = xlrd.open_workbook(xlsFile)
table = data.sheet_by_index(0)
ret = []
keys = table.row_values(0)
for rowNum in range(table.nrows):
oneRowValues = table.row_values(rowNum)
if rowNum > 0:
d = {}
for colIdx, key in enumerate(keys):
d[key] = oneRowValues[colIdx]
ret.append(d)
return ret
@classmethod
def readXlsToList(cls, xlsFile):
'''
读取xls文件生成list
'''
data = xlrd.open_workbook(xlsFile)
table = data.sheet_by_index(0)
ret = []
for rowNum in range(table.nrows):
oneRowValues = table.row_values(rowNum)
ret.append(oneRowValues)
return ret
@classmethod
def readTxt(cls, txtFile, sep):
'''
读取txt文件
'''
# with + open 可保证with语句执行完毕后同时关闭打开的文件句柄。
ret = []
with open(txtFile, "r") as f:
for line in f.readlines():
line = line.strip('\n') # 去掉换行符
listInfo = line.split(sep) # 以 sep 分割成数组
if listInfo:
ret.append(listInfo)
return ret
@classmethod
def writeToJson(cls, jsonFile, ret):
'''
写入json文件
'''
with open(jsonFile, 'w') as fp:
json.dump(ret, fp, indent=2, sort_keys=True, encoding="utf-8", ensure_ascii=False)
@classmethod
def writeFromStr(cls, filePath, s):
'''
string写入文件
'''
with open(filePath, 'w') as fp:
fp.write(s)
@classmethod
def writeFromList(cls, filePath, wList):
'''
list写入文件
'''
with open(filePath, 'w') as fp:
fp.writelines(wList)
if __name__ == "__main__":
obj = ObjectFileReadAndWrite()
# xls
ret = obj.readXlsToDict(xlsFile='xxx.xls')
obj.writeToJson('xxx.json', ret)
# txt
ret2 = obj.readTxt(txtFile='result.txt', sep=" ")
obj.writeToJson('result.json', ret2)