{"id":13714471,"url":"https://github.com/mapio/prettytable-mirror","last_synced_at":"2025-05-07T01:34:16.361Z","repository":{"id":26116370,"uuid":"29560798","full_name":"mapio/prettytable-mirror","owner":"mapio","description":"A mirror of the prettytable repo (on Google Code)","archived":false,"fork":false,"pushed_at":"2017-04-04T15:05:41.000Z","size":272,"stargazers_count":78,"open_issues_count":0,"forks_count":8,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-05-23T04:47:52.006Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://code.google.com/p/prettytable/","language":"Python","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mapio.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG","contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-01-20T23:28:50.000Z","updated_at":"2024-04-28T05:55:51.000Z","dependencies_parsed_at":"2022-08-25T17:10:38.443Z","dependency_job_id":null,"html_url":"https://github.com/mapio/prettytable-mirror","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mapio%2Fprettytable-mirror","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mapio%2Fprettytable-mirror/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mapio%2Fprettytable-mirror/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mapio%2Fprettytable-mirror/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mapio","download_url":"https://codeload.github.com/mapio/prettytable-mirror/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224551299,"owners_count":17330119,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T23:02:00.976Z","updated_at":"2024-11-14T01:31:18.608Z","avatar_url":"https://github.com/mapio.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"I am not the author of this software, this is just a mirror of [prettytable](http://code.google.com/p/prettytable) on [Google Code](http://code.google.com/); in particular, please report your issues on the [official issue tracker](http://code.google.com/p/prettytable/issues/list)!\n\n## TUTORIAL ON HOW TO USE THE PRETTYTABLE 0.6+ API\n\nThis tutorial is distributed with PrettyTable and is meant to serve\nas a \"quick start\" guide for the lazy or impatient.  It is not an\nexhaustive description of the whole API, and it is not guaranteed to be\n100% up to date.  For more complete and update documentation, check the\nPrettyTable wiki at http://code.google.com/p/prettytable/w/list\n\n### Getting your data into (and out of) the table\n\nLet's suppose you have a shiny new PrettyTable:\n\n```python\nfrom prettytable import PrettyTable\nx = PrettyTable()\n```\n\nand you want to put some data into it.  You have a few options.\n\n### Row by row\n\nYou can add data one row at a time.  To do this you can set the field names\nfirst using the `field_names` attribute, and then add the rows one at a time\nusing the `add_row` method:\n\n```python\nx.field_names = [\"City name\", \"Area\", \"Population\", \"Annual Rainfall\"]\nx.add_row([\"Adelaide\",1295, 1158259, 600.5])\nx.add_row([\"Brisbane\",5905, 1857594, 1146.4])\nx.add_row([\"Darwin\", 112, 120900, 1714.7])\nx.add_row([\"Hobart\", 1357, 205556, 619.5])\nx.add_row([\"Sydney\", 2058, 4336374, 1214.8])\nx.add_row([\"Melbourne\", 1566, 3806092, 646.9])\nx.add_row([\"Perth\", 5386, 1554769, 869.4])\n```\n\n### Column by column\n\nYou can add data one column at a time as well.  To do this you use the\n`add_column` method, which takes two arguments - a string which is the name for\nthe field the column you are adding corresponds to, and a list or tuple which\ncontains the column data\"\n\n```python\nx.add_column(\"City name\",\n[\"Adelaide\",\"Brisbane\",\"Darwin\",\"Hobart\",\"Sydney\",\"Melbourne\",\"Perth\"])\nx.add_column(\"Area\", [1295, 5905, 112, 1357, 2058, 1566, 5386])\nx.add_column(\"Population\", [1158259, 1857594, 120900, 205556, 4336374, 3806092,\n1554769])\nx.add_column(\"Annual Rainfall\",[600.5, 1146.4, 1714.7, 619.5, 1214.8, 646.9,\n869.4])\n```\n\n### Mixing and matching\n\nIf you really want to, you can even mix and match `add_row` and `add_column`\nand build some of your table in one way and some of it in the other.  There's a\nunit test which makes sure that doing things this way will always work out\nnicely as if you'd done it using just one of the two approaches.  Tables built\nthis way are kind of confusing for other people to read, though, so don't do\nthis unless you have a good reason.\n\n### Importing data from a CSV file\n\nIf you have your table data in a comma separated values file (.csv), you can\nread this data into a PrettyTable like this:\n\n```python\nfrom prettytable import from_csv\nfp = open(\"myfile.csv\", \"r\")\nmytable = from_csv(fp)\nfp.close()\n```\n\n### Importing data from a database cursor\n\nIf 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:\n\n```python\nimport sqlite3\nfrom prettytable import from_cursor\n\nconnection = sqlite3.connect(\"mydb.db\")\ncursor = connection.cursor()\ncursor.execute(\"SELECT field1, field2, field3 FROM my_table\")\nmytable = from_cursor(cursor)\n```\n\n### Getting data out\n\nThere are three ways to get data out of a PrettyTable, in increasing order of\ncompleteness:\n\n  * The `del_row` method takes an integer index of a single row to delete.\n  * The `clear_rows` method takes no arguments and deletes all the rows in the\ntable - but keeps the field names as they were so you that you can repopulate\nit with the same kind of data.\n  * The `clear` method takes no arguments and deletes all rows and all field\nnames.  It's not quite the same as creating a fresh table instance, though -\nstyle related settings, discussed later, are maintained.\n\n### Displaying your table in ASCII form\n\nPrettyTable's main goal is to let you print tables in an attractive ASCII form,\nlike this:\n\n```\n+-----------+------+------------+-----------------+\n| City name | Area | Population | Annual Rainfall |\n+-----------+------+------------+-----------------+\n| Adelaide  | 1295 |  1158259   |      600.5      |\n| Brisbane  | 5905 |  1857594   |      1146.4     |\n| Darwin    | 112  |   120900   |      1714.7     |\n| Hobart    | 1357 |   205556   |      619.5      |\n| Melbourne | 1566 |  3806092   |      646.9      |\n| Perth     | 5386 |  1554769   |      869.4      |\n| Sydney    | 2058 |  4336374   |      1214.8     |\n+-----------+------+------------+-----------------+\n```\n\nYou can print tables like this to `stdout` or get string representations of\nthem.\n\n### Printing\n\nTo print a table in ASCII form, you can just do this:\n\n`print x`\n\nin Python 2.x or:\n\n`print(x)`\n\nin Python 3.x.\n\nThe old `x.printt()` method from versions 0.5 and earlier has been removed.\n\nTo pass options changing the look of the table, use the `get_string()` method\ndocumented below:\n\n`print x.get_string()`\n\n### Stringing\n\nIf you don't want to actually print your table in ASCII form but just get a\nstring containing what _would_ be printed if you use `print x`, you can use\nthe `get_string` method:\n\n`mystring = x.get_string()`\n\nThis string is guaranteed to look exactly the same as what would be printed by\ndoing `print x`.  You can now do all the usual things you can do with a\nstring, like write your table to a file or insert it into a GUI.\n\n### Controlling which data gets displayed\n\nIf you like, you can restrict the output of `print x` or `x.get_string` to\nonly the fields or rows you like.\n\nThe `fields` argument to these methods takes a list of field names to be\nprinted:\n\n`print x.get_string(fields=[\"City name\", \"Population\"])`\n\ngives:\n\n```\n+-----------+------------+\n| City name | Population |\n+-----------+------------+\n| Adelaide  |  1158259   |\n| Brisbane  |  1857594   |\n| Darwin    |   120900   |\n| Hobart    |   205556   |\n| Melbourne |  3806092   |\n| Perth     |  1554769   |\n| Sydney    |  4336374   |\n+-----------+------------+\n```\n\nThe `start` and `end` arguments take the index of the first and last row to\nprint respectively.  Note that the indexing works like Python list slicing - to\nprint the 2nd, 3rd and 4th rows of the table, set `start` to 1 (the first row\nis row 0, so the second is row 1) and set `end` to 4 (the index of the 4th row,\nplus 1):\n\n`print x.get_string(start=1,end=4)`\n\nprints:\n\n```\n+-----------+------+------------+-----------------+\n| City name | Area | Population | Annual Rainfall |\n+-----------+------+------------+-----------------+\n| Brisbane  | 5905 |    1857594 | 1146.4          |\n| Darwin    | 112  |     120900 | 1714.7          |\n| Hobart    | 1357 |     205556 | 619.5           |\n+-----------+------+------------+-----------------+\n```\n\n## Changing the alignment of columns\n\nBy default, all columns in a table are centre aligned.\n\n### All columns at once\n\nYou can change the alignment of all the columns in a table at once by assigning\na one character string to the `align` attribute.  The allowed strings are \"l\",\n\"r\" and \"c\" for left, right and centre alignment, respectively:\n\n```python\nx.align = \"r\"\nprint x\n```\n\ngives:\n\n```\n+-----------+------+------------+-----------------+\n| City name | Area | Population | Annual Rainfall |\n+-----------+------+------------+-----------------+\n|  Adelaide | 1295 |    1158259 |           600.5 |\n|  Brisbane | 5905 |    1857594 |          1146.4 |\n|    Darwin |  112 |     120900 |          1714.7 |\n|    Hobart | 1357 |     205556 |           619.5 |\n| Melbourne | 1566 |    3806092 |           646.9 |\n|     Perth | 5386 |    1554769 |           869.4 |\n|    Sydney | 2058 |    4336374 |          1214.8 |\n+-----------+------+------------+-----------------+\n```\n\n### One column at a time\n\nYou can also change the alignment of individual columns based on the\ncorresponding field name by treating the `align` attribute as if it were a\ndictionary.\n\n```python\nx.align[\"City name\"] = \"l\"\nx.align[\"Area\"] = \"c\"\nx.align[\"Population\"] = \"r\"\nx.align[\"Annual Rainfall\"] = \"c\"\nprint x\n```\n\ngives:\n\n```\n+-----------+------+------------+-----------------+\n| City name | Area | Population | Annual Rainfall |\n+-----------+------+------------+-----------------+\n| Adelaide  | 1295 |    1158259 |      600.5      |\n| Brisbane  | 5905 |    1857594 |      1146.4     |\n| Darwin    | 112  |     120900 |      1714.7     |\n| Hobart    | 1357 |     205556 |      619.5      |\n| Melbourne | 1566 |    3806092 |      646.9      |\n| Perth     | 5386 |    1554769 |      869.4      |\n| Sydney    | 2058 |    4336374 |      1214.8     |\n+-----------+------+------------+-----------------+\n```\n\n## Sorting your table by a field\n\nYou can make sure that your ASCII tables are produced with the data sorted by\none particular field by giving `get_string` a `sortby` keyword argument, which\n must be a string containing the name of one field.\n\nFor example, to print the example table we built earlier of Australian capital\ncity data, so that the most populated city comes last, we can do this:\n\n`print x.get_string(sortby=\"Population\")`\n\nto get\n\n```\n+-----------+------+------------+-----------------+\n| City name | Area | Population | Annual Rainfall |\n+-----------+------+------------+-----------------+\n| Darwin    | 112  |   120900   |      1714.7     |\n| Hobart    | 1357 |   205556   |      619.5      |\n| Adelaide  | 1295 |  1158259   |      600.5      |\n| Perth     | 5386 |  1554769   |      869.4      |\n| Brisbane  | 5905 |  1857594   |      1146.4     |\n| Melbourne | 1566 |  3806092   |      646.9      |\n| Sydney    | 2058 |  4336374   |      1214.8     |\n+-----------+------+------------+-----------------+\n```\n\nIf we want the most populated city to come _first_, we can also give a\n`reversesort=True` argument.\n\nIf you _always_ want your tables to be sorted in a certain way, you can make\nthe setting long term like this:\n\n```python\nx.sortby = \"Population\"\nprint x\nprint x\nprint x\n```\n\nAll three tables printed by this code will be sorted by population (you could\ndo `x.reversesort = True` as well, if you wanted).  The behaviour will persist\nuntil you turn it off:\n\n`x.sortby = None`\n\nIf you want to specify a custom sorting function, you can use the `sort_key`\nkeyword argument.  Pass this a function which accepts two lists of values\nand returns a negative or positive value depending on whether the first list\nshould appeare before or after the second one.  If your table has n columns,\neach list will have n+1 elements.  Each list corresponds to one row of the\ntable.  The first element will be whatever data is in the relevant row, in\nthe column specified by the `sort_by` argument.  The remaining n elements\nare the data in each of the table's columns, in order, including a repeated\ninstance of the data in the `sort_by` column.\n\n## Changing the appearance of your table - the easy way\n\nBy default, PrettyTable produces ASCII tables that look like the ones used in\nSQL database shells.  But if can print them in a variety of other formats as\nwell.  If the format you want to use is common, PrettyTable makes this very\neasy for you to do using the `set_style` method.  If you want to produce an\nuncommon table, you'll have to do things slightly harder (see later).\n\n### Setting a table style\n\nYou can set the style for your table using the `set_style` method before any\ncalls to `print` or `get_string`.  Here's how to print a table in a format\nwhich works nicely with Microsoft Word's \"Convert to table\" feature:\n\n```python\nfrom prettytable import MSWORD_FRIENDLY\nx.set_style(MSWORD_FRIENDLY)\nprint x\n```\n\nIn addition to `MSWORD_FRIENDLY` there are currently two other in-built styles\nyou can use for your tables:\n\n  * `DEFAULT` - The default look, used to undo any style changes you may have\nmade\n  * `PLAIN_COLUMN` - A borderless style that works well with command line\nprograms for columnar data\n\nOther styles are likely to appear in future releases.\n\n## hanging the appearance of your table - the hard way\n\nIf you want to display your table in a style other than one of the in-built\nstyles listed above, you'll have to set things up the hard way.\n\nDon't worry, it's not really that hard!\n\n## Style options\n\nPrettyTable has a number of style options which control various aspects of how\ntables are displayed.  You have the freedom to set each of these options\nindividually to whatever you prefer.  The `set_style` method just does this\nautomatically for you.\n\nThe options are these:\n\n  * `border` - A boolean option (must be `True` or `False`).  Controls whether\n    or not a border is drawn around the table.\n  * `header` - A boolean option (must be `True` or `False`).  Controls whether\n    or not the first row of the table is a header showing the names of all the\n    fields.\n  * `hrules` - Controls printing of horizontal rules after rows.  Allowed\n    values: FRAME, HEADER, ALL, NONE - note that these are variables defined\n    inside the `prettytable` module so make sure you import them or use\n    `prettytable.FRAME` etc.\n  * `vrules` - Controls printing of vertical rules between columns.  Allowed\n    values: FRAME, ALL, NONE.\n  * `int_format` - A string which controls the way integer data is printed.\n    This works like: print \"%\u003cint_format\u003ed\" % data\n  * `float_format` - A string which controls the way floating point data is\n     printed.  This works like: print \"%\u003cint_format\u003ef\" % data\n  * `padding_width` - Number of spaces on either side of column data (only used\n    if left and right paddings are None).\n  * `left_padding_width` - Number of spaces on left hand side of column data.\n  * `right_padding_width` - Number of spaces on right hand side of column data.\n  * `vertical_char` - Single character string used to draw vertical lines.\n     Default is `|`.\n  * `horizontal_char` - Single character string used to draw horizontal lines.\n     Default is `-`.\n  * `junction_char` - Single character string used to draw line junctions.\n     Default is `+`.\n\nYou can set the style options to your own settings in two ways:\n\n## Setting style options for the long term\n\nIf you want to print your table with a different style several times, you can\nset your option for the \"long term\" just by changing the appropriate\nattributes.  If you never want your tables to have borders you can do this:\n\n```python\nx.border = False\nprint x\nprint x\nprint x\n```\n\nNeither of the 3 tables printed by this will have borders, even if you do\nthings like add extra rows inbetween them.  The lack of borders will last until\nyou do:\n\n`x.border = True`\n\nto turn them on again.  This sort of long term setting is exactly how\n`set_style` works.  `set_style` just sets a bunch of attributes to pre-set\nvalues for you.\n\nNote that if you know what style options you want at the moment you are\ncreating your table, you can specify them using keyword arguments to the\nconstructor.  For example, the following two code blocks are equivalent:\n\n```python\nx = PrettyTable()\nx.border = False\nx.header = False\nx.padding_width = 5\n\nx = PrettyTable(border=False, header=False, padding_width=5)\n```\n\n## Changing style options just once\n\nIf you don't want to make long term style changes by changing an attribute like\nin the previous section, you can make changes that last for just one\n``get_string`` by giving those methods keyword arguments.  To print two\n\"normal\" tables with one borderless table between them, you could do this:\n\n```python\nprint x\nprint x.get_string(border=False)\nprint x\n```\n\n## Displaying your table in HTML form\n\nPrettyTable will also print your tables in HTML form, as `\u003ctable\u003e`s.  Just like\nin ASCII form, you can actually print your table - just use `print_html()` - or\nget a string representation - just use `get_html_string()`.  HTML printing\nsupports the `fields`, `start`, `end`, `sortby` and `reversesort` arguments in\nexactly the same way as ASCII printing.\n\n### Styling HTML tables\n\nBy default, PrettyTable outputs HTML for \"vanilla\" tables.  The HTML code is\nquite simple.  It looks like this:\n\n```html\n\u003ctable\u003e\n    \u003ctr\u003e\n        \u003cth\u003eCity name\u003c/th\u003e\n        \u003cth\u003eArea\u003c/th\u003e\n        \u003cth\u003ePopulation\u003c/th\u003e\n        \u003cth\u003eAnnual Rainfall\u003c/th\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003eAdelaide\u003c/td\u003e\n        \u003ctd\u003e1295\u003c/td\u003e\n        \u003ctd\u003e1158259\u003c/td\u003e\n        \u003ctd\u003e600.5\u003c/td\u003e\n    \u003ctr\u003e\n        \u003ctd\u003eBrisbane\u003c/td\u003e\n        \u003ctd\u003e5905\u003c/td\u003e\n        \u003ctd\u003e1857594\u003c/td\u003e\n        \u003ctd\u003e1146.4\u003c/td\u003e\n    ...\n    ...\n    ...\n\u003c/table\u003e\n```\n\nIf you like, you can ask PrettyTable to do its best to mimick the style options\nthat your table has set using inline CSS.  This is done by giving a\n`format=True` keyword argument to either the `print_html` or `get_html_string`\nmethods.  Note that if you _always_ want to print formatted HTML you can do:\n\n`x.format = True`\n\nand the setting will persist until you turn it off.\n\nJust like with ASCII tables, if you want to change the table's style for just\none `print_html` or one `get_html_string` you can pass those methods keyword\narguments - exactly like `print` and `get_string`.\n\n## Setting HTML attributes\n\nYou can provide a dictionary of HTML attribute name/value pairs to the\n`print_html` and `get_html_string` methods using the `attributes` keyword\nargument.  This lets you specify common HTML attributes like `name`, `id` and\n`class` that can be used for linking to your tables or customising their\nappearance using CSS.  For example:\n\n`x.print_html(attributes={\"name\":\"my_table\", \"class\":\"red_table\"})`\n\nwill print:\n\n```html\n\u003ctable name=\"my_table\" class=\"red_table\"\u003e\n    \u003ctr\u003e\n        \u003cth\u003eCity name\u003c/th\u003e\n        \u003cth\u003eArea\u003c/th\u003e\n        \u003cth\u003ePopulation\u003c/th\u003e\n        \u003cth\u003eAnnual Rainfall\u003c/th\u003e\n    \u003c/tr\u003e\n    ...\n    ...\n    ...\n\u003c/table\u003e\n```\n\n## Miscellaneous things\n\n### Copying a table\n\nYou can call the `copy` method on a PrettyTable object without arguments to\nreturn an identical independent copy of the table.\n\nIf you want a copy of a PrettyTable object with just a subset of the rows,\nyou can use list slicing notation:\n\n`new_table = old_table[0:5]`\n\n\n![Analytics](https://ga-beacon.appspot.com/UA-377250-20/prettytable-mirror?pixel)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmapio%2Fprettytable-mirror","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmapio%2Fprettytable-mirror","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmapio%2Fprettytable-mirror/lists"}