Python实现的tab文件操作类分享
2014-11-20 15:53
501 查看
类代码:
# -*- coding:gbk -*- import os class TABFILE: def __init__(self, filename, dest_file = None): self.filename = filename if not dest_file: self.dest_file = filename else: self.dest_file = dest_file self.filehandle = None self.content = [] self.initflag = False self.column = 0 self.row = 0 self.data = [] def Init(self): try: self.filehandle = open(self.filename, 'r') self.initflag = self._load_file() except: pass else: self.initflag = True return self.initflag def UnInit(self): if self.initflag: self.filehandle.close() def _load_file(self): if self.filehandle: self.content = self.filehandle.readlines() self.row = len(self.content) - 1 head = self.content[0].split('\t') self.column = len(head) for line in self.content: #这里需要去掉末尾的换行 #line = line - '\n\r' self.data.append(line.rstrip().split('\t')) return True else: return False def GetValue(self, row, column): if 0 < row < self.row and 0 < column < self.column: return self.data[row][column - 1] else: return None def SetValue(self, row, column, value): if 0 < row < self.row and 0 < column < self.column: self.data[row][column] = value else: return False def SaveToFile(self): filewrite = open(self.dest_file, 'w') if not filewrite: return False sep_char = '\t' for line in self.data: filewrite.write(sep_char.join(line)+'\n') filewrite.close() return True
您可能感兴趣的文章:
相关文章推荐
- Python实现的tab文件操作类分享
- Python实现的ini文件操作类分享
- Python实现的ini文件操作类分享
- Python实现简单的文件传输与MySQL备份的脚本分享
- Python和perl实现批量对目录下电子书文件重命名的代码分享
- python脚本实现统计日志文件中的ip访问次数代码分享
- Python实现简单的文件传输与MySQL备份的脚本分享
- Python实现Linux主机与手机快速分享文件
- python脚本实现统计日志文件中的ip访问次数代码分享
- Python和perl实现批量对目录下电子书文件重命名的代码分享
- Python实现tab文件操作
- 使用ICSharpCode.SharpZipLib-(C#)实现解压缩文件的操作类
- python 实现生成目录树及显示文件大小
- 用python实现一个按需生成用于vim跳转的tags文件的小程序
- 用python 实现上传文件功能
- Java技巧:java实现文件监控技巧分享
- Python 实现文件复制、删除
- 【引用】Python open读写文件实现脚本
- python实现的一个文件同步模块
- python3实现带cookie的上传文件