python作为脚本性语言,加上它的简便易用性。会经常当作脚本用来处理一下数据和格式。其中处理文件就是频繁用处之一。简单编写几个常用的xls和txt读写函数,以后可以快速复用。
用到xlrd库函数需要预先install
命令:pip install xlrd
直接贴源码:
#! /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)
因文件中有中文,中间遇到中文乱码问题
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
# encoding="utf-8", ensure_ascii=False
1、这个是由于Unicode编码与ASCII编码的不兼容造成的。
2、通常都是ascii,由此Python自然调用ascii编码解码程序去处理字符流,当字符流不属于ascii范围内,就会抛出异常(ordinal not in range(128))
百度了下通过 以上方式 解决了