您的位置:首页 > 编程语言 > Python开发

python小工具___读取指定文件中特定字符串存入Excel表格

2017-06-07 14:33 155 查看
#coding:utf8
import os
import os.path
import xlwt
import xlrd
#  --- 获取当前路径 ---
filePath=os.getcwd()
#  --- 指定测试路径 ---
file01='F:\SVNBaloon\BalloonBlast\Assets\Scripts'
#  --- 开始特定标识 ---
startSign='Debug.Log("'
#  --- 结束特定标识 ---
endSign='");'
#  --- 创建表格 ---
getWordExcel=xlwt.Workbook()
#  --- 创建 Sheet ---
getTable=getWordExcel.add_sheet('getword',cell_overwrite_ok=True)
#  --- 行数 ---
index = 0
#  --- 遍历相应路径下的 parent :父目录 dirnames 所有文件夹 filenames 所有文件名 ---
for parent,dirnames,filenames in os.walk(filePath):
#  --- 遍历所有文件 ---
for filename in filenames:
#  --- 设置/获取 当前文件父目录 ---
totalFilePath=os.path.join(parent,filename)
#  --- 获取后缀为 .cs 或者 .txt的文件 ---
if filename.endswith('.cs') or filename.endswith('.lua'):
#  --- 获取文件全路径 ---
totalFile=os.path.abspath(totalFilePath)
#  --- 打开读取文件 'r' 为读取  'w' 为写入
fileInfo02=open(totalFilePath,'r')
#  --- 遍历当前文件查看每一行 ---
for file02 in fileInfo02:
#  --- 去掉每行换行符 '\n' ---
file02 = file02.strip('\n')
#  --- 判断开始标识 与 结束标识是否存在于当前行中 ---
if startSign in file02 and endSign in file02:
#  --- 进行切割操作 (说白了就是字符串的切割) ---
startIndex = file02.index(startSign)
if startIndex >= 0:
startIndex += len(startSign)
endIndex = file02.index(endSign)
#  --- 打印完成后获取到的字符串
print file02[startIndex:endIndex]
getWord=file02[startIndex:endIndex]
#  --- 对截取到的字符串进行转码 ---
newWord=unicode(getWord,'utf-8')
#  --- 写入表格第一列 ---
getTable.write(index,0,index+1)
#  --- 写入表格第二列 ---
getTable.write(index,1,newWord)
#  --- 行数自增 1 ---
index+=1
#  --- 判断该表格是否存在 ---
if os.path.isfile(filePath+'/WordXls.xls'):
os.remove(filePath+'/WordXls.xls')
getWordExcel.save(filePath+'/WordXls.xls')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: