METADATA 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. Metadata-Version: 2.1
  2. Name: prettytable
  3. Version: 1.0.1
  4. Summary: A simple Python library for easily displaying tabular data in a visually appealing ASCII table format
  5. Home-page: https://github.com/jazzband/prettytable
  6. Author: Luke Maurits
  7. Author-email: luke@maurits.id.au
  8. Maintainer: Jazzband
  9. License: BSD (3 clause)
  10. Project-URL: Source, https://github.com/jazzband/prettytable
  11. Platform: UNKNOWN
  12. Classifier: Programming Language :: Python
  13. Classifier: Programming Language :: Python :: 2
  14. Classifier: Programming Language :: Python :: 2.7
  15. Classifier: Programming Language :: Python :: 3
  16. Classifier: Programming Language :: Python :: 3.5
  17. Classifier: Programming Language :: Python :: 3.6
  18. Classifier: Programming Language :: Python :: 3.7
  19. Classifier: Programming Language :: Python :: 3.8
  20. Classifier: Programming Language :: Python :: 3.9
  21. Classifier: License :: OSI Approved :: BSD License
  22. Classifier: Topic :: Text Processing
  23. Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*
  24. Description-Content-Type: text/markdown
  25. Requires-Dist: setuptools
  26. Requires-Dist: wcwidth
  27. Provides-Extra: tests
  28. Requires-Dist: pytest ; extra == 'tests'
  29. Requires-Dist: pytest-cov ; extra == 'tests'
  30. # PrettyTable
  31. [![Jazzband](https://jazzband.co/static/img/badge.svg)](https://jazzband.co/)
  32. [![PyPI version](https://img.shields.io/pypi/v/prettytable.svg?logo=pypi&logoColor=FFE873)](https://pypi.org/project/prettytable/)
  33. [![Supported Python versions](https://img.shields.io/pypi/pyversions/prettytable.svg?logo=python&logoColor=FFE873)](https://pypi.org/project/prettytable/)
  34. [![PyPI downloads](https://img.shields.io/pypi/dm/prettytable.svg)](https://pypistats.org/packages/prettytable)
  35. [![Travis CI Status](https://img.shields.io/travis/jazzband/prettytable/master?label=Travis%20CI&logo=travis)](https://travis-ci.org/jazzband/prettytable)
  36. [![GitHub Actions status](https://github.com/jazzband/prettytable/workflows/Test/badge.svg)](https://github.com/jazzband/prettytable/actions)
  37. [![codecov](https://codecov.io/gh/jazzband/prettytable/branch/master/graph/badge.svg)](https://codecov.io/gh/jazzband/prettytable)
  38. [![Code style: Black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
  39. ## Installation
  40. Install via pip:
  41. pip install -U prettytable
  42. Install latest development version:
  43. pip install -U git+https://github.com/jazzband/prettytable
  44. Or from `requirements.txt`:
  45. -e git://github.com/jazzband/prettytable.git#egg=prettytable
  46. ## Tutorial on how to use the PrettyTable API
  47. ### Getting your data into (and out of) the table
  48. Let's suppose you have a shiny new PrettyTable:
  49. ```python
  50. from prettytable import PrettyTable
  51. x = PrettyTable()
  52. ```
  53. and you want to put some data into it. You have a few options.
  54. #### Row by row
  55. You can add data one row at a time. To do this you can set the field names
  56. first using the `field_names` attribute, and then add the rows one at a time
  57. using the `add_row` method:
  58. ```python
  59. x.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
  60. x.add_row(["Adelaide",1295, 1158259, 600.5])
  61. x.add_row(["Brisbane",5905, 1857594, 1146.4])
  62. x.add_row(["Darwin", 112, 120900, 1714.7])
  63. x.add_row(["Hobart", 1357, 205556, 619.5])
  64. x.add_row(["Sydney", 2058, 4336374, 1214.8])
  65. x.add_row(["Melbourne", 1566, 3806092, 646.9])
  66. x.add_row(["Perth", 5386, 1554769, 869.4])
  67. ```
  68. #### Column by column
  69. You can add data one column at a time as well. To do this you use the
  70. `add_column` method, which takes two arguments - a string which is the name for
  71. the field the column you are adding corresponds to, and a list or tuple which
  72. contains the column data"
  73. ```python
  74. x.add_column("City name",
  75. ["Adelaide","Brisbane","Darwin","Hobart","Sydney","Melbourne","Perth"])
  76. x.add_column("Area", [1295, 5905, 112, 1357, 2058, 1566, 5386])
  77. x.add_column("Population", [1158259, 1857594, 120900, 205556, 4336374, 3806092,
  78. 1554769])
  79. x.add_column("Annual Rainfall",[600.5, 1146.4, 1714.7, 619.5, 1214.8, 646.9,
  80. 869.4])
  81. ```
  82. #### Mixing and matching
  83. If you really want to, you can even mix and match `add_row` and `add_column`
  84. and build some of your table in one way and some of it in the other. There's a
  85. unit test which makes sure that doing things this way will always work out
  86. nicely as if you'd done it using just one of the two approaches. Tables built
  87. this way are kind of confusing for other people to read, though, so don't do
  88. this unless you have a good reason.
  89. #### Importing data from a CSV file
  90. If you have your table data in a comma separated values file (.csv), you can
  91. read this data into a PrettyTable like this:
  92. ```python
  93. from prettytable import from_csv
  94. fp = open("myfile.csv", "r")
  95. mytable = from_csv(fp)
  96. fp.close()
  97. ```
  98. #### Importing data from a database cursor
  99. If you have your table data in a database which you can access using a library which confirms to the Python DB-API (e.g. an SQLite database accessible using the sqlite module), then you can build a PrettyTable using a cursor object, like this:
  100. ```python
  101. import sqlite3
  102. from prettytable import from_cursor
  103. connection = sqlite3.connect("mydb.db")
  104. cursor = connection.cursor()
  105. cursor.execute("SELECT field1, field2, field3 FROM my_table")
  106. mytable = from_cursor(cursor)
  107. ```
  108. #### Getting data out
  109. There are three ways to get data out of a PrettyTable, in increasing order of
  110. completeness:
  111. * The `del_row` method takes an integer index of a single row to delete.
  112. * The `del_column` method takes a field name of a single column to delete.
  113. * The `clear_rows` method takes no arguments and deletes all the rows in the
  114. table - but keeps the field names as they were so you that you can repopulate
  115. it with the same kind of data.
  116. * The `clear` method takes no arguments and deletes all rows and all field
  117. names. It's not quite the same as creating a fresh table instance, though -
  118. style related settings, discussed later, are maintained.
  119. ### Displaying your table in ASCII form
  120. PrettyTable's main goal is to let you print tables in an attractive ASCII form,
  121. like this:
  122. ```
  123. +-----------+------+------------+-----------------+
  124. | City name | Area | Population | Annual Rainfall |
  125. +-----------+------+------------+-----------------+
  126. | Adelaide | 1295 | 1158259 | 600.5 |
  127. | Brisbane | 5905 | 1857594 | 1146.4 |
  128. | Darwin | 112 | 120900 | 1714.7 |
  129. | Hobart | 1357 | 205556 | 619.5 |
  130. | Melbourne | 1566 | 3806092 | 646.9 |
  131. | Perth | 5386 | 1554769 | 869.4 |
  132. | Sydney | 2058 | 4336374 | 1214.8 |
  133. +-----------+------+------------+-----------------+
  134. ```
  135. You can print tables like this to `stdout` or get string representations of
  136. them.
  137. #### Printing
  138. To print a table in ASCII form, you can just do this:
  139. ```python
  140. print x
  141. ```
  142. in Python 2.x or:
  143. ```python
  144. print(x)
  145. ```
  146. in Python 3.x.
  147. The old `x.printt()` method from versions 0.5 and earlier has been removed.
  148. To pass options changing the look of the table, use the `get_string()` method
  149. documented below:
  150. ```python
  151. print(x.get_string())
  152. ```
  153. #### Stringing
  154. If you don't want to actually print your table in ASCII form but just get a
  155. string containing what _would_ be printed if you use `print(x)`, you can use
  156. the `get_string` method:
  157. ```python
  158. mystring = x.get_string()
  159. ```
  160. This string is guaranteed to look exactly the same as what would be printed by
  161. doing `print(x)`. You can now do all the usual things you can do with a
  162. string, like write your table to a file or insert it into a GUI.
  163. #### Controlling which data gets displayed
  164. If you like, you can restrict the output of `print(x)` or `x.get_string` to
  165. only the fields or rows you like.
  166. The `fields` argument to these methods takes a list of field names to be
  167. printed:
  168. ```python
  169. print(x.get_string(fields=["City name", "Population"]))
  170. ```
  171. gives:
  172. ```
  173. +-----------+------------+
  174. | City name | Population |
  175. +-----------+------------+
  176. | Adelaide | 1158259 |
  177. | Brisbane | 1857594 |
  178. | Darwin | 120900 |
  179. | Hobart | 205556 |
  180. | Melbourne | 3806092 |
  181. | Perth | 1554769 |
  182. | Sydney | 4336374 |
  183. +-----------+------------+
  184. ```
  185. The `start` and `end` arguments take the index of the first and last row to
  186. print respectively. Note that the indexing works like Python list slicing - to
  187. print the 2nd, 3rd and 4th rows of the table, set `start` to 1 (the first row
  188. is row 0, so the second is row 1) and set `end` to 4 (the index of the 4th row,
  189. plus 1):
  190. ```python
  191. print(x.get_string(start=1,end=4))
  192. ```
  193. prints:
  194. ```
  195. +-----------+------+------------+-----------------+
  196. | City name | Area | Population | Annual Rainfall |
  197. +-----------+------+------------+-----------------+
  198. | Brisbane | 5905 | 1857594 | 1146.4 |
  199. | Darwin | 112 | 120900 | 1714.7 |
  200. | Hobart | 1357 | 205556 | 619.5 |
  201. +-----------+------+------------+-----------------+
  202. ```
  203. #### Changing the alignment of columns
  204. By default, all columns in a table are centre aligned.
  205. ##### All columns at once
  206. You can change the alignment of all the columns in a table at once by assigning
  207. a one character string to the `align` attribute. The allowed strings are "l",
  208. "r" and "c" for left, right and centre alignment, respectively:
  209. ```python
  210. x.align = "r"
  211. print(x)
  212. ```
  213. gives:
  214. ```
  215. +-----------+------+------------+-----------------+
  216. | City name | Area | Population | Annual Rainfall |
  217. +-----------+------+------------+-----------------+
  218. | Adelaide | 1295 | 1158259 | 600.5 |
  219. | Brisbane | 5905 | 1857594 | 1146.4 |
  220. | Darwin | 112 | 120900 | 1714.7 |
  221. | Hobart | 1357 | 205556 | 619.5 |
  222. | Melbourne | 1566 | 3806092 | 646.9 |
  223. | Perth | 5386 | 1554769 | 869.4 |
  224. | Sydney | 2058 | 4336374 | 1214.8 |
  225. +-----------+------+------------+-----------------+
  226. ```
  227. ##### One column at a time
  228. You can also change the alignment of individual columns based on the
  229. corresponding field name by treating the `align` attribute as if it were a
  230. dictionary.
  231. ```python
  232. x.align["City name"] = "l"
  233. x.align["Area"] = "c"
  234. x.align["Population"] = "r"
  235. x.align["Annual Rainfall"] = "c"
  236. print(x)
  237. ```
  238. gives:
  239. ```
  240. +-----------+------+------------+-----------------+
  241. | City name | Area | Population | Annual Rainfall |
  242. +-----------+------+------------+-----------------+
  243. | Adelaide | 1295 | 1158259 | 600.5 |
  244. | Brisbane | 5905 | 1857594 | 1146.4 |
  245. | Darwin | 112 | 120900 | 1714.7 |
  246. | Hobart | 1357 | 205556 | 619.5 |
  247. | Melbourne | 1566 | 3806092 | 646.9 |
  248. | Perth | 5386 | 1554769 | 869.4 |
  249. | Sydney | 2058 | 4336374 | 1214.8 |
  250. +-----------+------+------------+-----------------+
  251. ```
  252. ##### Sorting your table by a field
  253. You can make sure that your ASCII tables are produced with the data sorted by
  254. one particular field by giving `get_string` a `sortby` keyword argument, which
  255. must be a string containing the name of one field.
  256. For example, to print the example table we built earlier of Australian capital
  257. city data, so that the most populated city comes last, we can do this:
  258. ```python
  259. print(x.get_string(sortby="Population"))
  260. ```
  261. to get
  262. ```
  263. +-----------+------+------------+-----------------+
  264. | City name | Area | Population | Annual Rainfall |
  265. +-----------+------+------------+-----------------+
  266. | Darwin | 112 | 120900 | 1714.7 |
  267. | Hobart | 1357 | 205556 | 619.5 |
  268. | Adelaide | 1295 | 1158259 | 600.5 |
  269. | Perth | 5386 | 1554769 | 869.4 |
  270. | Brisbane | 5905 | 1857594 | 1146.4 |
  271. | Melbourne | 1566 | 3806092 | 646.9 |
  272. | Sydney | 2058 | 4336374 | 1214.8 |
  273. +-----------+------+------------+-----------------+
  274. ```
  275. If we want the most populated city to come _first_, we can also give a
  276. `reversesort=True` argument.
  277. If you _always_ want your tables to be sorted in a certain way, you can make
  278. the setting long term like this:
  279. ```python
  280. x.sortby = "Population"
  281. print(x)
  282. print(x)
  283. print(x)
  284. ```
  285. All three tables printed by this code will be sorted by population (you could
  286. do `x.reversesort = True` as well, if you wanted). The behaviour will persist
  287. until you turn it off:
  288. ```python
  289. x.sortby = None
  290. ```
  291. If you want to specify a custom sorting function, you can use the `sort_key`
  292. keyword argument. Pass this a function which accepts two lists of values
  293. and returns a negative or positive value depending on whether the first list
  294. should appear before or after the second one. If your table has n columns,
  295. each list will have n+1 elements. Each list corresponds to one row of the
  296. table. The first element will be whatever data is in the relevant row, in
  297. the column specified by the `sort_by` argument. The remaining n elements
  298. are the data in each of the table's columns, in order, including a repeated
  299. instance of the data in the `sort_by` column.
  300. ### Changing the appearance of your table - the easy way
  301. By default, PrettyTable produces ASCII tables that look like the ones used in
  302. SQL database shells. But if can print them in a variety of other formats as
  303. well. If the format you want to use is common, PrettyTable makes this very
  304. easy for you to do using the `set_style` method. If you want to produce an
  305. uncommon table, you'll have to do things slightly harder (see later).
  306. #### Setting a table style
  307. You can set the style for your table using the `set_style` method before any
  308. calls to `print` or `get_string`. Here's how to print a table in a format
  309. which works nicely with Microsoft Word's "Convert to table" feature:
  310. ```python
  311. from prettytable import MSWORD_FRIENDLY
  312. x.set_style(MSWORD_FRIENDLY)
  313. print(x)
  314. ```
  315. In addition to `MSWORD_FRIENDLY` there are currently two other in-built styles
  316. you can use for your tables:
  317. * `DEFAULT` - The default look, used to undo any style changes you may have
  318. made
  319. * `PLAIN_COLUMNS` - A borderless style that works well with command line
  320. programs for columnar data
  321. * `MARKDOWN` - A style that follows Markdown syntax
  322. * `ORGMODE` - A table style that fits [Org mode](https://orgmode.org/) syntax
  323. Other styles are likely to appear in future releases.
  324. ### Changing the appearance of your table - the hard way
  325. If you want to display your table in a style other than one of the in-built
  326. styles listed above, you'll have to set things up the hard way.
  327. Don't worry, it's not really that hard!
  328. #### Style options
  329. PrettyTable has a number of style options which control various aspects of how
  330. tables are displayed. You have the freedom to set each of these options
  331. individually to whatever you prefer. The `set_style` method just does this
  332. automatically for you.
  333. The options are these:
  334. * `border` - A boolean option (must be `True` or `False`). Controls whether
  335. or not a border is drawn around the table.
  336. * `header` - A boolean option (must be `True` or `False`). Controls whether
  337. or not the first row of the table is a header showing the names of all the
  338. fields.
  339. * `hrules` - Controls printing of horizontal rules after rows. Allowed
  340. values: FRAME, HEADER, ALL, NONE - note that these are variables defined
  341. inside the `prettytable` module so make sure you import them or use
  342. `prettytable.FRAME` etc.
  343. * `vrules` - Controls printing of vertical rules between columns. Allowed
  344. values: FRAME, ALL, NONE.
  345. * `int_format` - A string which controls the way integer data is printed.
  346. This works like: `print("%<int_format>d" % data)`
  347. * `float_format` - A string which controls the way floating point data is
  348. printed. This works like: `print("%<float_format>f" % data)`
  349. * `padding_width` - Number of spaces on either side of column data (only used
  350. if left and right paddings are None).
  351. * `left_padding_width` - Number of spaces on left hand side of column data.
  352. * `right_padding_width` - Number of spaces on right hand side of column data.
  353. * `vertical_char` - Single character string used to draw vertical lines.
  354. Default is `|`.
  355. * `horizontal_char` - Single character string used to draw horizontal lines.
  356. Default is `-`.
  357. * `junction_char` - Single character string used to draw line junctions.
  358. Default is `+`.
  359. You can set the style options to your own settings in two ways:
  360. #### Setting style options for the long term
  361. If you want to print your table with a different style several times, you can
  362. set your option for the "long term" just by changing the appropriate
  363. attributes. If you never want your tables to have borders you can do this:
  364. ```python
  365. x.border = False
  366. print(x)
  367. print(x)
  368. print(x)
  369. ```
  370. Neither of the 3 tables printed by this will have borders, even if you do
  371. things like add extra rows in between them. The lack of borders will last until
  372. you do:
  373. ```python
  374. x.border = True
  375. ```
  376. to turn them on again. This sort of long term setting is exactly how
  377. `set_style` works. `set_style` just sets a bunch of attributes to pre-set
  378. values for you.
  379. Note that if you know what style options you want at the moment you are
  380. creating your table, you can specify them using keyword arguments to the
  381. constructor. For example, the following two code blocks are equivalent:
  382. ```python
  383. x = PrettyTable()
  384. x.border = False
  385. x.header = False
  386. x.padding_width = 5
  387. x = PrettyTable(border=False, header=False, padding_width=5)
  388. ```
  389. #### Changing style options just once
  390. If you don't want to make long term style changes by changing an attribute like
  391. in the previous section, you can make changes that last for just one
  392. ``get_string`` by giving those methods keyword arguments. To print two
  393. "normal" tables with one borderless table between them, you could do this:
  394. ```python
  395. print(x)
  396. print(x.get_string(border=False))
  397. print(x)
  398. ```
  399. ### Displaying your table in JSON
  400. PrettyTable will also print your tables in JSON, as a list of fields and an array
  401. of rows. Just like in ASCII form, you can actually get a string representation - just use
  402. `get_json_string()`.
  403. ### Displaying your table in HTML form
  404. PrettyTable will also print your tables in HTML form, as `<table>`s. Just like
  405. in ASCII form, you can actually get a string representation - just use
  406. `get_html_string()`. HTML printing supports the `fields`, `start`, `end`,
  407. `sortby` and `reversesort` arguments in exactly the same way as ASCII printing.
  408. #### Styling HTML tables
  409. By default, PrettyTable outputs HTML for "vanilla" tables. The HTML code is
  410. quite simple. It looks like this:
  411. ```html
  412. <table>
  413. <tr>
  414. <th>City name</th>
  415. <th>Area</th>
  416. <th>Population</th>
  417. <th>Annual Rainfall</th>
  418. </tr>
  419. <tr>
  420. <td>Adelaide</td>
  421. <td>1295</td>
  422. <td>1158259</td>
  423. <td>600.5</td>
  424. <tr>
  425. <td>Brisbane</td>
  426. <td>5905</td>
  427. <td>1857594</td>
  428. <td>1146.4</td>
  429. ...
  430. ...
  431. ...
  432. </table>
  433. ```
  434. If you like, you can ask PrettyTable to do its best to mimic the style options
  435. that your table has set using inline CSS. This is done by giving a
  436. `format=True` keyword argument to `get_html_string` method. Note that if you
  437. _always_ want to print formatted HTML you can do:
  438. ```python
  439. x.format = True
  440. ```
  441. and the setting will persist until you turn it off.
  442. Just like with ASCII tables, if you want to change the table's style for just
  443. one `get_html_string` you can pass those methods keyword arguments - exactly
  444. like `print` and `get_string`.
  445. #### Setting HTML attributes
  446. You can provide a dictionary of HTML attribute name/value pairs to the
  447. `get_html_string` method using the `attributes` keyword argument.
  448. This lets you specify common HTML attributes like `name`, `id` and
  449. `class` that can be used for linking to your tables or customising their
  450. appearance using CSS. For example:
  451. ```python
  452. print(x.get_html_string(attributes={"name":"my_table", "class":"red_table"}))
  453. ```
  454. will print:
  455. ```html
  456. <table name="my_table" class="red_table">
  457. <tr>
  458. <th>City name</th>
  459. <th>Area</th>
  460. <th>Population</th>
  461. <th>Annual Rainfall</th>
  462. </tr>
  463. ...
  464. ...
  465. ...
  466. </table>
  467. ```
  468. ### Miscellaneous things
  469. #### Copying a table
  470. You can call the `copy` method on a PrettyTable object without arguments to
  471. return an identical independent copy of the table.
  472. If you want a copy of a PrettyTable object with just a subset of the rows,
  473. you can use list slicing notation:
  474. ```python
  475. new_table = old_table[0:5]
  476. ```
  477. ## Contributing
  478. After editing files, use the [black](https://github.com/psf/black) linter to auto-format changed lines.
  479. ```sh
  480. pip install black
  481. black prettytable*.py
  482. ```