自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python實(shí)現(xiàn)tab文件操作相關(guān)應(yīng)用方式解讀

開發(fā) 后端
Python實(shí)現(xiàn)tab文件操作在實(shí)際應(yīng)用中是一個(gè)比較重要的操作技巧。那么,我們將會(huì)在這里通過一段代碼示例來為大家詳細(xì)介紹一下相關(guān)操作方法。

Python編程語言的應(yīng)用范圍比較廣泛,可以在很多領(lǐng)域中來幫助開發(fā)人員實(shí)現(xiàn)一些特定的需求。比如在文件操作方面。我們今天就為大家詳細(xì)介紹一下有Python實(shí)現(xiàn)tab文件操作的一些具體操作技巧。

Python實(shí)現(xiàn)tab文件操作代碼示例:

  1. # -*- coding:gbk -*-  
  2. import os  
  3. class TABFILE:  
  4. def __init__(self, filename, dest_file = None):  
  5. self.filename = filename  
  6. if not dest_file:  
  7. self.dest_file = filename 
  8. else:  
  9. self.dest_file = dest_file  
  10. self.filehandle = None 
  11. self.content = []  
  12. self.initflag = False 
  13. self.column = 0 
  14. self.row = 0 
  15. self.data = []  
  16. def Init(self):  
  17. try:   
  18. self.filehandle = open(self.filename, 'r')  
  19. selfself.initflag = self._load_file()  
  20. except:   
  21. pass  
  22. else:  
  23. self.initflag = True 
  24. return self.initflag  
  25. def UnInit(self):  
  26. if self.initflag:  
  27. self.filehandle.close()  
  28. def _load_file(self):  
  29. if self.filehandle:  
  30. selfself.content = self.filehandle.readlines()  
  31. self.row = len(self.content) - 1  
  32. head = self.content[0].split('\t')  
  33. self.column = len(head)  
  34. for line in self.content:  
  35. #這里需要去掉末尾的換行  
  36. #lineline = line - '\n\r'  
  37. self.data.append(line.rstrip().split('\t'))  
  38. return True  
  39. else:  
  40. return False  
  41. def GetValue(self, row, column):  
  42. if 0 < row < self.row and 0 < column < self.column: 
  43. return self.data[row][column - 1]  
  44. else:  
  45. return None  
  46. def SetValue(self, row, column, value):  
  47. if 0 < row < self.row and 0 < column < self.column: 
  48. self.data[row][column] = value  
  49. else:  
  50. return False  
  51. def SaveToFile(self):  
  52. filewrite = open(self.dest_file, 'w')  
  53. if not filewrite:  
  54. return False  
  55. sep_char = '\t' 
  56. for line in self.data:  
  57. filewrite.write(sep_char.join(line)+'\n')  
  58. filewrite.close()  
  59. return True 

以上就是我們對(duì)Python實(shí)現(xiàn)tab文件操作的相關(guān)介紹。

【編輯推薦】

  1. Python實(shí)現(xiàn)ini文件操作基本操作方式分享
  2. Python構(gòu)造列表基本應(yīng)用語法詳解
  3. 利用PDB實(shí)現(xiàn)Python程序調(diào)試
  4. Python單元測(cè)試正確使用規(guī)則
  5. Python SQLITE數(shù)據(jù)庫(kù)操作簡(jiǎn)便易用
責(zé)任編輯:曹凱 來源: 博客園
相關(guān)推薦

2010-03-03 13:45:08

Python查找重復(fù)文

2009-12-28 15:00:21

ADO操作

2010-03-04 15:57:23

Python實(shí)現(xiàn)ini

2010-03-04 11:36:02

Python提交表單

2024-10-11 12:00:00

Python批量文件操作

2010-03-03 14:30:05

Python set類

2009-12-16 13:33:28

Ruby輸出文件信息

2010-02-02 17:39:31

C++構(gòu)造函數(shù)

2010-03-03 17:33:52

Python讀取XML

2010-02-05 16:46:58

C++ TinyXml

2009-12-22 10:15:17

ADO.NET規(guī)則

2010-03-03 15:17:46

Python調(diào)用MyS

2010-03-03 16:40:55

Python HTTP

2010-03-03 13:22:08

Python正則表達(dá)式

2010-03-03 13:32:08

Python壓縮文件

2010-02-26 13:26:55

WCF消息編碼器

2010-03-05 10:47:05

Python futu

2009-12-30 16:45:31

ADO操作

2010-03-17 14:42:09

Python 文件

2010-03-23 16:30:47

Python文件復(fù)制
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)