How to Implement tab file operations in Python

Source: Internet
Author: User

The Python programming language is widely used and can be used in many fields to help developers meet certain requirements. For example, in file operations. Today, we will give you a detailed introduction of some specific operation skills for implementing the tab file operation in Python.

Python code example for implementing the tab file operation:

 
 
  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 t:
  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. # Remove the line feed at the end.
  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

The preceding section describes how to implement tab file operations in Python.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.