用于创建简单ascii表的python模块。

texttable · PyPI

  1. NAME
  2. texttable - module for creating simple ASCII tables
  3. FILE
  4. /usr/local/lib/python2.7/dist-packages/texttable.py
  5. DESCRIPTION
  6. Example:
  7. table = Texttable()
  8. table.set_cols_align(["l", "r", "c"])
  9. table.set_cols_valign(["t", "m", "b"])
  10. table.add_rows([["Name", "Age", "Nickname"],
  11. ["Mr\nXavier\nHuon", 32, "Xav'"],
  12. ["Mr\nBaptiste\nClement", 1, "Baby"],
  13. ["Mme\nLouise\nBourgeau", 28, "Lou\n\nLoue"]])
  14. print table.draw() + "\n"
  15. table = Texttable()
  16. table.set_deco(Texttable.HEADER)
  17. table.set_cols_dtype(['t', # text
  18. 'f', # float (decimal)
  19. 'e', # float (exponent)
  20. 'i', # integer
  21. 'a']) # automatic
  22. table.set_cols_align(["l", "r", "r", "r", "l"])
  23. table.add_rows([["text", "float", "exp", "int", "auto"],
  24. ["abcd", "67", 654, 89, 128.001],
  25. ["efghijk", 67.5434, .654, 89.6, 12800000000000000000000.00023],
  26. ["lmn", 5e-78, 5e-78, 89.4, .000000000000128],
  27. ["opqrstu", .023, 5e+78, 92., 12800000000000000000000]])
  28. print table.draw()
  29. Result:
  30. +----------+-----+----------+
  31. | Name | Age | Nickname |
  32. +==========+=====+==========+
  33. | Mr | | |
  34. | Xavier | 32 | |
  35. | Huon | | Xav' |
  36. +----------+-----+----------+
  37. | Mr | | |
  38. | Baptiste | 1 | |
  39. | Clement | | Baby |
  40. +----------+-----+----------+
  41. | Mme | | Lou |
  42. | Louise | 28 | |
  43. | Bourgeau | | Loue |
  44. +----------+-----+----------+
  45. text float exp int auto
  46. ==============================================
  47. abcd 67.000 6.540e+02 89 128.001
  48. efghijk 67.543 6.540e-01 90 1.280e+22
  49. lmn 0.000 5.000e-78 89 0.000
  50. opqrstu 0.023 5.000e+78 92 1.280e+22
  51. CLASSES
  52. class Texttable
  53. | Methods defined here:
  54. |
  55. | __init__(self, max_width=80)
  56. | Constructor
  57. |
  58. | - max_width is an integer, specifying the maximum width of the table
  59. | - if set to 0, size is unlimited, therefore cells won't be wrapped
  60. |
  61. | add_row(self, array)
  62. | Add a row in the rows stack
  63. |
  64. | - cells can contain newlines and tabs
  65. |
  66. | add_rows(self, rows, header=True)
  67. | Add several rows in the rows stack
  68. |
  69. | - The 'rows' argument can be either an iterator returning arrays,
  70. | or a by-dimensional array
  71. | - 'header' specifies if the first row should be used as the header
  72. | of the table
  73. |
  74. | draw(self)
  75. | Draw the table
  76. |
  77. | - the table is returned as a whole string
  78. |
  79. | header(self, array)
  80. | Specify the header of the table
  81. |
  82. | reset(self)
  83. | Reset the instance
  84. |
  85. | - reset rows and header
  86. |
  87. | set_chars(self, array)
  88. | Set the characters used to draw lines between rows and columns
  89. |
  90. | - the array should contain 4 fields:
  91. |
  92. | [horizontal, vertical, corner, header]
  93. |
  94. | - default is set to:
  95. |
  96. | ['-', '|', '+', '=']
  97. |
  98. | set_cols_align(self, array)
  99. | Set the desired columns alignment
  100. |
  101. | - the elements of the array should be either "l", "c" or "r":
  102. |
  103. | * "l": column flushed left
  104. | * "c": column centered
  105. | * "r": column flushed right
  106. |
  107. | set_cols_dtype(self, array)
  108. | Set the desired columns datatype for the cols.
  109. |
  110. | - the elements of the array should be either a callable or any of
  111. | "a", "t", "f", "e" or "i":
  112. |
  113. | * "a": automatic (try to use the most appropriate datatype)
  114. | * "t": treat as text
  115. | * "f": treat as float in decimal format
  116. | * "e": treat as float in exponential format
  117. | * "i": treat as int
  118. | * a callable: should return formatted string for any value given
  119. |
  120. | - by default, automatic datatyping is used for each column
  121. |
  122. | set_cols_valign(self, array)
  123. | Set the desired columns vertical alignment
  124. |
  125. | - the elements of the array should be either "t", "m" or "b":
  126. |
  127. | * "t": column aligned on the top of the cell
  128. | * "m": column aligned on the middle of the cell
  129. | * "b": column aligned on the bottom of the cell
  130. |
  131. | set_cols_width(self, array)
  132. | Set the desired columns width
  133. |
  134. | - the elements of the array should be integers, specifying the
  135. | width of each column. For example:
  136. |
  137. | [10, 20, 5]
  138. |
  139. | set_deco(self, deco)
  140. | Set the table decoration
  141. |
  142. | - 'deco' can be a combinaison of:
  143. |
  144. | Texttable.BORDER: Border around the table
  145. | Texttable.HEADER: Horizontal line below the header
  146. | Texttable.HLINES: Horizontal lines between rows
  147. | Texttable.VLINES: Vertical lines between columns
  148. |
  149. | All of them are enabled by default
  150. |
  151. | - example:
  152. |
  153. | Texttable.BORDER | Texttable.HEADER
  154. |
  155. | set_header_align(self, array)
  156. | Set the desired header alignment
  157. |
  158. | - the elements of the array should be either "l", "c" or "r":
  159. |
  160. | * "l": column flushed left
  161. | * "c": column centered
  162. | * "r": column flushed right
  163. |
  164. | set_max_width(self, max_width)
  165. | Set the maximum width of the table
  166. |
  167. | - max_width is an integer, specifying the maximum width of the table
  168. | - if set to 0, size is unlimited, therefore cells won't be wrapped
  169. |
  170. | set_precision(self, width)
  171. | Set the desired precision for float/exponential formats
  172. |
  173. | - width must be an integer >= 0
  174. |
  175. | - default value is set to 3
  176. |
  177. | ----------------------------------------------------------------------
  178. | Data and other attributes defined here:
  179. |
  180. | BORDER = 1
  181. |
  182. | HEADER = 2
  183. |
  184. | HLINES = 4
  185. |
  186. | VLINES = 8
  187. DATA
  188. __all__ = ['Texttable', 'ArraySizeError']
  189. __author__ = 'Gerome Fournier <jef(at)foutaise.org>'
  190. __credits__ = 'Jeff Kowalczyk:\n - textwrap improved import\n ...at...
  191. __license__ = 'MIT'
  192. __version__ = '1.6.2'
  193. VERSION
  194. 1.6.2
  195. AUTHOR
  196. Gerome Fournier <jef(at)foutaise.org>
  197. CREDITS
  198. Jeff Kowalczyk:
  199. - textwrap improved import
  200. - comment concerning header output
  201. Anonymous:
  202. - add_rows method, for adding rows in one go
  203. Sergey Simonenko:
  204. - redefined len() function to deal with non-ASCII characters
  205. Roger Lew:
  206. - columns datatype specifications
  207. Brian Peterson:
  208. - better handling of unicode errors
  209. Frank Sachsenheim:
  210. - add Python 2/3-compatibility
  211. Maximilian Hils:
  212. - fix minor bug for Python 3 compatibility
  213. frinkelpi:
  214. - preserve empty lines