vegaseat

prettytable a list of lists

Mar 13th, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.08 KB | None | 0 0
  1. ''' prettytable103.py
  2. explore module prettytable with list of lists data
  3.  
  4. get
  5. prettytable-0.7.2.zip
  6. from
  7. https://pypi.python.org/pypi/PrettyTable
  8. extract the zip file t\and copy prettytable.py to /Lib/side-packages
  9.  
  10. '''
  11.  
  12. from prettytable import PrettyTable
  13.  
  14. # created from article in:
  15. # http://pypl.github.io/PYPL.html
  16. header = ['Rank', 'Language', 'Share(%)', 'Trend']
  17. lang_list = [
  18. ['1', 'Java', '24.7', '-0.4'],
  19. ['2', 'PHP', '11.7', '-1.2'],
  20. ['3', 'Python', '10.6', '+0.9'],
  21. ['4', 'C#', '8.9', '-0.3'],
  22. ['5', 'C++', '8.2', '-0.5'],
  23. ['6', 'C', '7.8', '+0.1'],
  24. ['7', 'Javascript', '7.2', '-0.3'],
  25. ['8', 'Objective-C', '6.1', '-0.2'],
  26. ['9', 'Matlab', '3.0', '-0.2'],
  27. ['10', 'R', '2.7', '+0.6'],
  28. ['11', 'Ruby', '2.5', '+0.0'],
  29. ['12', 'Swift', '2.5', '+2.9'],
  30. ['13', 'Visual Basic', '2.3', '-0.7'],
  31. ['14', 'Perl', '1.3', '-0.3'],
  32. ['15', 'lua', '0.5', '-0.1']]
  33.  
  34. pt = PrettyTable(header)
  35. # left align Language column (center is default)
  36. pt.align["Language"] = "l"
  37. # right align Rank column values
  38. pt.align["Rank"] = "r"
  39. # space between column edges and contents (1 = default)
  40. pt.padding_width = 1
  41.  
  42. for item in lang_list:
  43.     pt.add_row(item)
  44.  
  45. print(pt)
  46. print("(Trend is in % vs a year ago)")
  47.  
  48. ''' result ...
  49. +------+--------------+----------+-------+
  50. | Rank | Language     | Share(%) | Trend |
  51. +------+--------------+----------+-------+
  52. |    1 | Java         |   24.7   |  -0.4 |
  53. |    2 | PHP          |   11.7   |  -1.2 |
  54. |    3 | Python       |   10.6   |  +0.9 |
  55. |    4 | C#           |   8.9    |  -0.3 |
  56. |    5 | C++          |   8.2    |  -0.5 |
  57. |    6 | C            |   7.8    |  +0.1 |
  58. |    7 | Javascript   |   7.2    |  -0.3 |
  59. |    8 | Objective-C  |   6.1    |  -0.2 |
  60. |    9 | Matlab       |   3.0    |  -0.2 |
  61. |   10 | R            |   2.7    |  +0.6 |
  62. |   11 | Ruby         |   2.5    |  +0.0 |
  63. |   12 | Swift        |   2.5    |  +2.9 |
  64. |   13 | Visual Basic |   2.3    |  -0.7 |
  65. |   14 | Perl         |   1.3    |  -0.3 |
  66. |   15 | lua          |   0.5    |  -0.1 |
  67. +------+--------------+----------+-------+
  68. (Trend is in % vs a year ago)
  69. '''
Advertisement
Add Comment
Please, Sign In to add comment