Install Prettytable Python

broken image


  1. Yum Install Python-prettytable
  2. Install Prettytable Python Online
  3. Apt-get Install Python3-prettytable
  4. Install Prettytable Python Software
  5. Install Prettytable Python Download

Prettytable python title; prettytable in pytho;n; preetytable python column width; prettytable python example; python libary for creating tables; import prettytable; prettytable mac; install prettyTable python 3.9; how to install prettytable python on 3.9; pip install prettytable; datascroller prettytable python; install prettytable. Prettytable - Installation.wiki - Google Code Archive, The easiest way to install PrettyTable is to use the 'easyinstall' command which is part of Python setuputils. From the command line, you can simply type. Conda install linux-64 v0.7.2; win-32 v0.7.2; noarch v0.7.2; win-64 v0.7.2; osx-64 v0.7.2; To install this package with conda run one.

Latest version

Released:

A simple Python library for easily displaying tabular data in a visually appealing ASCII table format

Project description

PTable is a simple Python library designed to make it quick and easy torepresent tabular data in visually appealing ASCII tables, originallyforked from PrettyTable .

This tutorial is distributed with PrettyTable and is meant to serve asa 'quick start' guide for the lazy or impatient. It is not an exhaustivedescription of the whole API, and it is not guaranteed to be 100% up todate. For more complete and update documentation, check the PrettyTablewiki at http://code.google.com/p/prettytable/w/list

Getting your data into (and out of) the table

Let's suppose you have a shiny new PrettyTable:

and you want to put some data into it. You have a few options.

Row by row

You can add data one row at a time. To do this you can set the fieldnames first using the field_names attribute, and then add the rowsone at a time using the add_row method:

Column by column

You can add data one column at a time as well. To do this you use theadd_column method, which takes two arguments - a string which is thename for the field the column you are adding corresponds to, and a listor tuple which contains the column data'

Mixing and matching

Prettytable

If you really want to, you can even mix and match add_row andadd_column and build some of your table in one way and some of it inthe other. There's a unit test which makes sure that doing things thisway will always work out nicely as if you'd done it using just one ofthe two approaches. Tables built this way are kind of confusing forother people to read, though, so don't do this unless you have a goodreason.

Importing data from a CSV file

If you have your table data in a comma separated values file (.csv), youcan read this data into a PrettyTable like this:

Importing data from a database cursor

If you have your table data in a database which you can access using alibrary which confirms to the Python DB-API (e.g. an SQLite databaseaccessible using the sqlite module), then you can build a PrettyTableusing a cursor object, like this:

Getting data out

There are three ways to get data out of a PrettyTable, in increasingorder of completeness:

  • The del_row method takes an integer index of a single row todelete.
  • The clear_rows method takes no arguments and deletes all the rowsin the table - but keeps the field names as they were so you that youcan repopulate it with the same kind of data.
  • The clear method takes no arguments and deletes all rows and allfield names. It's not quite the same as creating a fresh tableinstance, though - style related settings, discussed later, aremaintained.

Displaying your table in ASCII form

PrettyTable's main goal is to let you print tables in an attractiveASCII form, like this:

You can print tables like this to stdout or get stringrepresentations of them.

Printing

To print a table in ASCII form, you can just do this:

in Python 2.x or:

in Python 3.x.

The old x.printt() method from versions 0.5 and earlier has beenremoved.

To pass options changing the look of the table, use the get_string()method documented below:

Install Prettytable Python

Stringing

If you don't want to actually print your table in ASCII form but justget a string containing what would be printed if you use 'print x',you can use the get_string method:

This string is guaranteed to look exactly the same as what would beprinted by doing 'print x'. You can now do all the usual things you cando with a string, like write your table to a file or insert it into aGUI.

Controlling which data gets displayed

If you like, you can restrict the output of print x orx.get_string to only the fields or rows you like.

The fields argument to these methods takes a list of field names tobe printed:

gives:

The start and end arguments take the index of the first and lastrow to print respectively. Note that the indexing works like Python listslicing - to print the 2nd, 3rd and 4th rows of the table, set startto 1 (the first row is row 0, so the second is row 1) and set end to4 (the index of the 4th row, plus 1):

prints:

Changing the alignment of columns

By default, all columns in a table are centre aligned.

All columns at once

You can change the alignment of all the columns in a table at once byassigning a one character string to the align attribute. The allowedstrings are 'l', 'r' and 'c' for left, right and centre alignment,respectively:

gives:

One column at a time

You can also change the alignment of individual columns based on thecorresponding field name by treating the align attribute as if itwere a dictionary.

gives:

Sorting your table by a field

You can make sure that your ASCII tables are produced with the datasorted by one particular field by giving get_string a sortbykeyword argument, which > must be a string containing the name of onefield.

For example, to print the example table we built earlier of Australiancapital city data, so that the most populated city comes last, we can dothis:

to get

If we want the most populated city to come first, we can also give areversesort=True argument.

If you always want your tables to be sorted in a certain way, you canmake the setting long term like this:

All three tables printed by this code will be sorted by population (youcould do x.reversesort = True as well, if you wanted). The behaviourwill persist until you turn it off:

If you want to specify a custom sorting function, you can use thesort_key keyword argument. Pass this a function which accepts twolists of values and returns a negative or positive value depending onwhether the first list should appeare before or after the second one. Ifyour table has n columns, each list will have n+1 elements. Each listcorresponds to one row of the table. The first element will be whateverdata is in the relevant row, in the column specified by the sort_byargument. The remaining n elements are the data in each of the table'scolumns, in order, including a repeated instance of the data in thesort_by column.

Changing the appearance of your table - the easy way

By default, PrettyTable produces ASCII tables that look like the onesused in SQL database shells. But if can print them in a variety of otherformats as well. If the format you want to use is common, PrettyTablemakes this very easy for you to do using the set_style method. Ifyou want to produce an uncommon table, you'll have to do things slightlyharder (see later).

Setting a table style

Yum Install Python-prettytable

You can set the style for your table using the set_style methodbefore any calls to print or get_string. Here's how to print atable in a format which works nicely with Microsoft Word's 'Convert totable' feature:

In addition to MSWORD_FRIENDLY there are currently two otherin-built styles you can use for your tables:

  • DEFAULT - The default look, used to undo any style changes youmay have made
  • PLAIN_COLUMN - A borderless style that works well with commandline programs for columnar data

Other styles are likely to appear in future releases.

Changing the appearance of your table - the hard way

If you want to display your table in a style other than one of thein-built styles listed above, you'll have to set things up the hard way.

Don't worry, it's not really that hard!

Style options

PrettyTable has a number of style options which control various aspectsof how tables are displayed. You have the freedom to set each of theseoptions individually to whatever you prefer. The set_style methodjust does this automatically for you.

The options are these:

  • border - A boolean option (must be True or False).Controls whether > > or not a border is drawn around the table.
  • header - A boolean option (must be True or False).Controls whether > > or not the first row of the table is a headershowing the names of all the > > fields.
  • hrules - Controls printing of horizontal rules after rows.Allowed > > values: FRAME, HEADER, ALL, NONE - note that these arevariables defined > > inside the prettytable module so make sureyou import them or use > > prettytable.FRAME etc.
  • vrules - Controls printing of vertical rules between columns.Allowed > > values: FRAME, ALL, NONE.
  • int_format - A string which controls the way integer data isprinted. > > This works like: print '%d' % data
  • float_format - A string which controls the way floating pointdata is > > printed. This works like:print '%f' % data
  • padding_width - Number of spaces on either side of column data(only used > > if left and right paddings are None).
  • left_padding_width - Number of spaces on left hand side of columndata.
  • right_padding_width - Number of spaces on right hand side ofcolumn data.
  • vertical_char - Single character string used to draw verticallines. > > Default is |.
  • horizontal_char - Single character string used to draw horizontallines. > > Default is -.
  • junction_char - Single character string used to draw linejunctions. > > Default is +.

You can set the style options to your own settings in two ways:

Setting style options for the long term

If you want to print your table with a different style several times,you can set your option for the 'long term' just by changing theappropriate attributes. If you never want your tables to have bordersyou can do this:

Neither of the 3 tables printed by this will have borders, even if youdo things like add extra rows inbetween them. The lack of borders willlast until you do:

to turn them on again. This sort of long term setting is exactly howset_style works. set_style just sets a bunch of attributes topre-set values for you.

Note that if you know what style options you want at the moment you arecreating your table, you can specify them using keyword arguments to theconstructor. For example, the following two code blocks are equivalent:

x = PrettyTable(border=False, header=False, padding_width=5)

Changing style options just once

If you don't want to make long term style changes by changing anattribute like in the previous section, you can make changes that lastfor just one get_string by giving those methods keyword arguments.To print two 'normal' tables with one borderless table between them, youcould do this:

Displaying your table in HTML form

PrettyTable will also print your tables in HTML form, as s.Just like in ASCII form, you can actually print your table - just useprint_html() - or get a string representation - just useget_html_string(). HTML printing supports the fields, start,end, sortby and reversesort arguments in exactly the sameway as ASCII printing.

Styling HTML tables

By default, PrettyTable outputs HTML for 'vanilla' tables. The HTML codeis quite simple. It looks like this:

If you like, you can ask PrettyTable to do its best to mimick the styleoptions that your table has set using inline CSS. This is done by givinga format=True keyword argument to either the print_html orget_html_string methods. Note that if you always want to printformatted HTML you can do:

Install prettytable python ubuntu

If you really want to, you can even mix and match add_row andadd_column and build some of your table in one way and some of it inthe other. There's a unit test which makes sure that doing things thisway will always work out nicely as if you'd done it using just one ofthe two approaches. Tables built this way are kind of confusing forother people to read, though, so don't do this unless you have a goodreason.

Importing data from a CSV file

If you have your table data in a comma separated values file (.csv), youcan read this data into a PrettyTable like this:

Importing data from a database cursor

If you have your table data in a database which you can access using alibrary which confirms to the Python DB-API (e.g. an SQLite databaseaccessible using the sqlite module), then you can build a PrettyTableusing a cursor object, like this:

Getting data out

There are three ways to get data out of a PrettyTable, in increasingorder of completeness:

  • The del_row method takes an integer index of a single row todelete.
  • The clear_rows method takes no arguments and deletes all the rowsin the table - but keeps the field names as they were so you that youcan repopulate it with the same kind of data.
  • The clear method takes no arguments and deletes all rows and allfield names. It's not quite the same as creating a fresh tableinstance, though - style related settings, discussed later, aremaintained.

Displaying your table in ASCII form

PrettyTable's main goal is to let you print tables in an attractiveASCII form, like this:

You can print tables like this to stdout or get stringrepresentations of them.

Printing

To print a table in ASCII form, you can just do this:

in Python 2.x or:

in Python 3.x.

The old x.printt() method from versions 0.5 and earlier has beenremoved.

To pass options changing the look of the table, use the get_string()method documented below:

Stringing

If you don't want to actually print your table in ASCII form but justget a string containing what would be printed if you use 'print x',you can use the get_string method:

This string is guaranteed to look exactly the same as what would beprinted by doing 'print x'. You can now do all the usual things you cando with a string, like write your table to a file or insert it into aGUI.

Controlling which data gets displayed

If you like, you can restrict the output of print x orx.get_string to only the fields or rows you like.

The fields argument to these methods takes a list of field names tobe printed:

gives:

The start and end arguments take the index of the first and lastrow to print respectively. Note that the indexing works like Python listslicing - to print the 2nd, 3rd and 4th rows of the table, set startto 1 (the first row is row 0, so the second is row 1) and set end to4 (the index of the 4th row, plus 1):

prints:

Changing the alignment of columns

By default, all columns in a table are centre aligned.

All columns at once

You can change the alignment of all the columns in a table at once byassigning a one character string to the align attribute. The allowedstrings are 'l', 'r' and 'c' for left, right and centre alignment,respectively:

gives:

One column at a time

You can also change the alignment of individual columns based on thecorresponding field name by treating the align attribute as if itwere a dictionary.

gives:

Sorting your table by a field

You can make sure that your ASCII tables are produced with the datasorted by one particular field by giving get_string a sortbykeyword argument, which > must be a string containing the name of onefield.

For example, to print the example table we built earlier of Australiancapital city data, so that the most populated city comes last, we can dothis:

to get

If we want the most populated city to come first, we can also give areversesort=True argument.

If you always want your tables to be sorted in a certain way, you canmake the setting long term like this:

All three tables printed by this code will be sorted by population (youcould do x.reversesort = True as well, if you wanted). The behaviourwill persist until you turn it off:

If you want to specify a custom sorting function, you can use thesort_key keyword argument. Pass this a function which accepts twolists of values and returns a negative or positive value depending onwhether the first list should appeare before or after the second one. Ifyour table has n columns, each list will have n+1 elements. Each listcorresponds to one row of the table. The first element will be whateverdata is in the relevant row, in the column specified by the sort_byargument. The remaining n elements are the data in each of the table'scolumns, in order, including a repeated instance of the data in thesort_by column.

Changing the appearance of your table - the easy way

By default, PrettyTable produces ASCII tables that look like the onesused in SQL database shells. But if can print them in a variety of otherformats as well. If the format you want to use is common, PrettyTablemakes this very easy for you to do using the set_style method. Ifyou want to produce an uncommon table, you'll have to do things slightlyharder (see later).

Setting a table style

Yum Install Python-prettytable

You can set the style for your table using the set_style methodbefore any calls to print or get_string. Here's how to print atable in a format which works nicely with Microsoft Word's 'Convert totable' feature:

In addition to MSWORD_FRIENDLY there are currently two otherin-built styles you can use for your tables:

  • DEFAULT - The default look, used to undo any style changes youmay have made
  • PLAIN_COLUMN - A borderless style that works well with commandline programs for columnar data

Other styles are likely to appear in future releases.

Changing the appearance of your table - the hard way

If you want to display your table in a style other than one of thein-built styles listed above, you'll have to set things up the hard way.

Don't worry, it's not really that hard!

Style options

PrettyTable has a number of style options which control various aspectsof how tables are displayed. You have the freedom to set each of theseoptions individually to whatever you prefer. The set_style methodjust does this automatically for you.

The options are these:

  • border - A boolean option (must be True or False).Controls whether > > or not a border is drawn around the table.
  • header - A boolean option (must be True or False).Controls whether > > or not the first row of the table is a headershowing the names of all the > > fields.
  • hrules - Controls printing of horizontal rules after rows.Allowed > > values: FRAME, HEADER, ALL, NONE - note that these arevariables defined > > inside the prettytable module so make sureyou import them or use > > prettytable.FRAME etc.
  • vrules - Controls printing of vertical rules between columns.Allowed > > values: FRAME, ALL, NONE.
  • int_format - A string which controls the way integer data isprinted. > > This works like: print '%d' % data
  • float_format - A string which controls the way floating pointdata is > > printed. This works like:print '%f' % data
  • padding_width - Number of spaces on either side of column data(only used > > if left and right paddings are None).
  • left_padding_width - Number of spaces on left hand side of columndata.
  • right_padding_width - Number of spaces on right hand side ofcolumn data.
  • vertical_char - Single character string used to draw verticallines. > > Default is |.
  • horizontal_char - Single character string used to draw horizontallines. > > Default is -.
  • junction_char - Single character string used to draw linejunctions. > > Default is +.

You can set the style options to your own settings in two ways:

Setting style options for the long term

If you want to print your table with a different style several times,you can set your option for the 'long term' just by changing theappropriate attributes. If you never want your tables to have bordersyou can do this:

Neither of the 3 tables printed by this will have borders, even if youdo things like add extra rows inbetween them. The lack of borders willlast until you do:

to turn them on again. This sort of long term setting is exactly howset_style works. set_style just sets a bunch of attributes topre-set values for you.

Note that if you know what style options you want at the moment you arecreating your table, you can specify them using keyword arguments to theconstructor. For example, the following two code blocks are equivalent:

x = PrettyTable(border=False, header=False, padding_width=5)

Changing style options just once

If you don't want to make long term style changes by changing anattribute like in the previous section, you can make changes that lastfor just one get_string by giving those methods keyword arguments.To print two 'normal' tables with one borderless table between them, youcould do this:

Displaying your table in HTML form

PrettyTable will also print your tables in HTML form, as s.Just like in ASCII form, you can actually print your table - just useprint_html() - or get a string representation - just useget_html_string(). HTML printing supports the fields, start,end, sortby and reversesort arguments in exactly the sameway as ASCII printing.

Styling HTML tables

By default, PrettyTable outputs HTML for 'vanilla' tables. The HTML codeis quite simple. It looks like this:

If you like, you can ask PrettyTable to do its best to mimick the styleoptions that your table has set using inline CSS. This is done by givinga format=True keyword argument to either the print_html orget_html_string methods. Note that if you always want to printformatted HTML you can do:

and the setting will persist until you turn it off.

Just like with ASCII tables, if you want to change the table's style forjust one print_html or one get_html_string you can pass thosemethods keyword arguments - exactly like print and get_string.

Setting HTML attributes

You can provide a dictionary of HTML attribute name/value pairs to theprint_html and get_html_string methods using the attributeskeyword argument. This lets you specify common HTML attributes likename, id and class that can be used for linking to yourtables or customising their appearance using CSS. For example:

will print:

Miscellaneous things

Copying a table

You can call the copy method on a PrettyTable object withoutarguments to return an identical independent copy of the table.

If you want a copy of a PrettyTable object with just a subset of therows, you can use list slicing notation:

Release historyRelease notifications | RSS feed

0.9.2

0.9.1

0.9.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for PTable, version 0.9.2
Filename, sizeFile typePython versionUpload dateHashes
Filename, size PTable-0.9.2.tar.gz (31.0 kB) File type Source Python version None Upload dateHashes
Close

Hashes for PTable-0.9.2.tar.gz

Hashes for PTable-0.9.2.tar.gz
AlgorithmHash digest
SHA256aa7fc151cb40f2dabcd2275ba6f7fd0ff8577a86be3365cd3fb297cbe09cc292
MD54a68135b1fc878d13d9df1a41cf4c5ac
BLAKE2-256abb3b54301811173ca94119eb474634f120a49cd370f257d1aae5a4abaf12729

When we click an image using a digital camera, or our mobile devices. The camera uses the Exchangeable Image File Format (EXIF) standards to store the Image. The EXIF standards use the metatags to specify the information about the image itself.

And if you want to access that metadata for the EXIF Standard Images in Python then you need to use an Image Handling Python Library. To handle Images in Python we generally use the most popular Python Image handling Libary Pillow.

Here in this Python tutorial, I will walk you through the Python program that will extract the useful metadata from the Digital Image.

So before getting started with the Python program let's Install the required Library.

Install Python pillow Library

Install Prettytable Python Online

Pillow is the defacto Python Image handling library, and many Python image processing and machine learning libraries are built on the pillow.

To install pillow for your Python environment run the following pip install command on your Command Prompt(Windows) or Terminal(Linux/macOS)

Install Python prettytable Library

The prettytable is a Python open-source library that is used to print data in a tabular format. We will be using this Library in this tutorial to print all the metadata information in a table format.

To install prettytable for your Python Environment use the following pip install command.

For this tutorial, I will be using the following Image image.jpgthat I have clicked with my mobile device.

How to Extract Image Metadata in Python?

Now open your best Python IDE or Text editor and follow along.

Apt-get Install Python3-prettytable

Let's start with importing the Pillow required modules.

Now set a Python Identifierimage_filename which holds the name file name of the Image. And also initialize the prettytable library's PrettyTable() module object, and set its fields.

Install Prettytable Python Software

Now load the image in our Python script using the Image module.

Now let's get the EXIF data of the loaded image using getexif() method.

Install Prettytable Python Download

The getexif() method only returns the tags IDs, and their values, not the tag names, that's why we have also imported the PIL.ExifTags TAGS that can retrieve the EXIF tag name based on its ID. Now we will loop through the IDs present in the img_exif_data and get their tag names using the TAGS.get() method and values using img_exif_data.get() method.

Some of the EXIF data present in the binary format (bytes), that's why we have decoded it into the human-readable format using data.decode() method.

Now put all the code together and execute

#Python program to Extract metadata from an Image File.

Output

From the output, You can see that the program print all the metadata about the image. It also displays the GPS coordinates where the image had been clicked.

Conclusion

In this Python tutorial, you learned 'How to extract MetaData information of an Image in Python'. The MetaData contains all the information based on the EXIF Standards. I would suggest you use the mobile clicked image when you are extracting the EXIF metadata information. You can also extract the Geolocation coordinates from the image and write a Python program to find the physical address of the clicked image.





broken image