• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python实现的tab文件操作类分享

Python实现的tab文件操作类分享

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了python tab,python tab补全,python tab键,python tab符号,python split tab等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

类代码:

# -*- 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

</div>

您可能想查找下面的文章:

  • Python使用tablib生成excel文件的简单实现方法
  • Python实现Tab自动补全和历史命令管理的方法
  • Python实现的tab文件操作类分享
  • Python实现的tab文件操作类分享

相关文章

  • Python运算符重载详解及实例代码
  • Python自动化运维和部署项目工具Fabric使用实例
  • 详解python 字符串和日期之间转换 StringAndDate
  • 浅析Python的web.py框架中url的设定方法
  • Python自动化测试ConfigParser模块读写配置文件
  • python base64 decode incorrect padding错误解决方法
  • Django的session中对于用户验证的支持
  • python 实现归并排序算法
  • 浅谈Python浅拷贝、深拷贝及引用机制
  • python3中set(集合)的语法总结分享

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 简介Django中内置的一些中间件
    • Python中给List添加元素的4种方法分享
    • 使用Python装饰器在Django框架下去除冗余代码的教程
    • Python字符串和文件操作常用函数分析
    • Python实现在线音乐播放器
    • Python 返回汉字的汉语拼音
    • Python中MySQL数据迁移到MongoDB脚本的方法
    • Python进阶篇之字典操作总结
    • 总结python实现父类调用两种方法的不同
    • python基础教程之类class定义使用方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有