返回顶部
首页 > 资讯 > 后端开发 > Python >python 打印表单格式
  • 409
分享到

python 打印表单格式

表单格式python 2023-01-31 02:01:01 409人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

对于老版本的python可能有兼容问题 Some places still have legacy environments that are slowly migrating to newer versions. I was abl

对于老版本的python可能有兼容问题
Some places still have legacy environments that are slowly migrating to newer versions. I was able to resolve it by just removing the unicode_literals and utf. Thanks for the response!
https://bitbucket.org/astanin/Python-tabulate/issue/4/__new__-keyWords-must-be-strings-in-linux
这样改过之后直接用其文件而不用库,就可以解决问题。

Download
tabulate-0.6.tar.gz

Pretty-print tabular data

Pretty-print tabular data in Python.

The main use cases of the library are:

  • printing small tables without hassle: just one function call,fORMatting is guided by the data itself
  • authoring tabular data for lightweight plain-text markup: multipleoutput formats suitable for further editing or transformation
  • readable presentation of mixed textual and numeric data: smartcolumn alignment, configurable number formatting, alignment by adecimal point
pip install tabulate

The module provides just one function, tabulate, which takes alist of lists or another tabular data type as the first argument,and outputs a nicely formatted plain-text table:

>>> from tabulate import tabulate

>>> table = [["Sun",696000,1989100000],["Earth",6371,5973.6],
...          ["Moon",1737,73.5],["Mars",3390,641.85]]
>>> print tabulate(table)
-----  ------  -------------
Sun    696000     1.9891e+09
Earth    6371  5973.6
Moon     1737    73.5
Mars     3390   641.85
-----  ------  -------------

The following tabular data types are supported:

  • list of lists or another iterable of iterables
  • dict of iterables
  • two-dimensional NumPy array
  • pandas.DataFrame

Examples in this file use Python2. Tabulate supports python3 too (Python >= 3.3).

Headers

The second optional argument named headers defines a list ofcolumn headers to be used:

>>> print tabulate(table, headers=["Planet","R (km)", "mass (x 10^29 kg)"])
Planet      R (km)    mass (x 10^29 kg)
--------  --------  -------------------
Sun         696000           1.9891e+09
Earth         6371        5973.6
Moon          1737          73.5
Mars          3390         641.85

If headers="firstrow", then the first row of data is used:

>>> print tabulate([["Name","Age"],["Alice",24],["Bob",19]],
...                headers="firstrow")
Name      Age
------  -----
Alice      24
Bob        19

If headers="keys", then the keys of a dictionary/dataframe,or column indices are used:

>>> print tabulate({"Name": ["Alice", "Bob"],
...                 "Age": [24, 19]}, headers="keys")
  Age  Name
-----  ------
   24  Alice
   19  Bob

Table format

There is more than one way to format a table in plain text.The third optional argument namedtablefmt defineshow the table is formatted.

Supported table formats are:

  • "plain"
  • "simple"
  • "grid"
  • "pipe"
  • "orgtbl"
  • "rst"
  • "mediawiki"

plain tables do not use any pseudo-graphics to draw lines:

>>> table = [["spam",42],["eggs",451],["bacon",0]]
>>> headers = ["item", "Qty"]
>>> print tabulate(table, headers, tablefmt="plain")
item      qty
spam       42
eggs      451
bacon       0

simple is the default format (the default may change in futureversions). It corresponds tosimple_tables in Pandoc markdownextensions:

>>> print tabulate(table, headers, tablefmt="simple")
item      qty
------  -----
spam       42
eggs      451
bacon       0

grid is like tables formatted by EMacs' table.elpackage. It corresponds to grid_tables in Pandoc Markdownextensions:

>>> print tabulate(table, headers, tablefmt="grid")
+--------+-------+
| item   |   qty |
+========+=======+
| spam   |    42 |
+--------+-------+
| eggs   |   451 |
+--------+-------+
| bacon  |     0 |
+--------+-------+

pipe follows the conventions of PHP Markdown Extra extension. Itcorresponds topipe_tables in Pandoc. This format uses colons toindicate column alignment:

>>> print tabulate(table, headers, tablefmt="pipe")
| item   |   qty |
|:-------|------:|
| spam   |    42 |
| eggs   |   451 |
| bacon  |     0 |

orgtbl follows the conventions of Emacs org-mode, and is editablealso in the minor orgtbl-mode. Hence its name:

>>> print tabulate(table, headers, tablefmt="orgtbl")
| item   |   qty |
|--------+-------|
| spam   |    42 |
| eggs   |   451 |
| bacon  |     0 |

rst formats data like a simple table of the reStructuredText format:

>>> print tabulate(table, headers, tablefmt="rst")
======  =====
item      qty
======  =====
spam       42
eggs      451
bacon       0
======  =====

mediawiki format produces a table markup used inWikipedia and onother MediaWiki-based sites:

>>> print tabulate(table, headers, tablefmt="mediawiki")
{| class="wikitable" style="text-align: left;"
|+ <!-- caption -->
|-
! item   !! align="right"|   qty
|-
| spam   || align="right"|    42
|-
| eggs   || align="right"|   451
|-
| bacon  || align="right"|     0
|}

Column alignment

tabulate is smart about column alignment. It detects columns whichcontain only numbers, and aligns them by a decimal point (or flushesthem to the right if they appear to be integers). Text columns areflushed to the left.

You can override the default alignment with numalign andstralign named arguments. Possible column alignments are:right,center,left, decimal (only for numbers).

Aligning by a decimal point works best when you need to comparenumbers at a glance:

>>> print tabulate([[1.2345],[123.45],[12.345],[12345],[1234.5]])
----------
    1.2345
  123.45
   12.345
12345
 1234.5
----------

Compare this with a more common right alignment:

>>> print tabulate([[1.2345],[123.45],[12.345],[12345],[1234.5]], numalign="right")
------
1.2345
123.45
12.345
 12345
1234.5
------

For tabulate, anything which can be parsed as a number is anumber. Even numbers represented as strings are aligned properly. Thisfeature comes in handy when reading a mixed table of text and numbersfrom a file:

>>> import csv ; from StringIO import StringIO
>>> table = list(csv.reader(StringIO("spam, 42\neggs, 451\n")))
>>> table
[['spam', ' 42'], ['eggs', ' 451']]
>>> print tabulate(table)
----  ----
spam    42
eggs   451
----  ----

Number formatting

tabulate allows to define custom number formatting applied to allcolumns of decimal numbers. Usefloatfmt named argument:

>>> print tabulate([["pi",3.141593],["e",2.718282]], floatfmt=".4f")
--  ------
pi  3.1416
e   2.7183
--  ------

Such features as decimal point alignment and trying to parse everythingas a number imply thattabulate:

  • has to "guess" how to print a particular tabular data type
  • needs to keep the entire table in-memory
  • has to "transpose" the table twice
  • does much more work than it may appear

It may not be suitable for serializing really big tables (but who'sGoing to do that, anyway?) or printing tables in performance sensitiveapplications.tabulate is about two orders of magnitude slowerthan simply joining lists of values with a tab, coma or otherseparator.

In the same time tabulate is comparable to other tablepretty-printers. Given a 10x10 table (a list of lists) of mixed textand numeric data,tabulate appears to be slower thanasciitable, and faster thanPrettyTable and texttable

===========================  ==========  ===========
Table formatter                time, μs    rel. time
===========================  ==========  ===========
join with tabs and newlines        34.7          1.0
csv to StringIO                    49.1          1.4
asciitable (0.8.0)               1272.1         36.7
tabulate (0.6)                   1964.5         56.6
PrettyTable (0.7.1)              5678.9        163.7
texttable (0.8.1)                6005.2        173.1
===========================  ==========  ===========
  • 0.6: mediawiki tables, bug fixes
  • 0.5.1: Fix README.rst formatting. Optimize (performance similar to 0.4.4).
  • 0.5: ANSI color sequences. Printing dicts of iterables and Pandas' dataframes.
  • 0.4.4: Python 2.6 support
  • 0.4.3: Bug fix, None as a missing value
  • 0.4.2: Fix manifest file
  • 0.4.1: Update license and documentation.
  • 0.4: Unicode support, Python3 support, rst tables
  • 0.3: Initial PyPI release. Table formats: simple,plain,grid, pipe, and orgtbl.
 
File Type Py Version Uploaded on Size
tabulate-0.6.tar.gz (md5) Source   2013-08-09 14KB
 
  • Downloads (All Versions):
  • 45 downloads in the last day
  • 223 downloads in the last week
  • 676 downloads in the last month
  • Author: Sergey Astanin
  • Categories
    • Development Status :: 4 - Beta
    • License :: OSI Approved :: MIT License
    • Operating System :: OS Independent
    • Programming Language :: Python :: 2.6
    • Programming Language :: Python :: 2.7
    • Programming Language :: Python :: 3.3
    • Topic :: Software Development :: Libraries

--结束END--

本文标题: python 打印表单格式

本文链接: https://lsjlt.com/news/186372.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
  • python 打印表单格式
    对于老版本的python可能有兼容问题 Some places still have legacy environments that are slowly migrating to newer versions. I was abl...
    99+
    2023-01-31
    表单 格式 python
  • Python 表格打印
    Python编程快速上手实践项目题目,欢迎指证与优化!编写一个名为 printTable()的函数, 它接受字符串的列表的列表,将它显示在组织良好的表格中, 每列右对齐。假定所有内层列表都包含同样数目的字符串。例如,该值可能看起来像这样:t...
    99+
    2023-01-31
    表格 Python
  • Python不同格式打印九九乘法表示例
    目录前言:1.长方形完整格式2.左上三角形3.右上三角形4.左下三角形5.右下三角形前言: 最近在学习Python,学习资源有慕课网上的视频教程、菜鸟教程以及Python官方文档tu...
    99+
    2024-04-02
  • ASP.NET MVC打印表格并实现部分视图表格打印
    假设在一个页面上有众多内容,而我们只想把该页面上的表格内容打印出来,window.print()方法会把整个页面的内容打印出来,如何做到只打印表格内容呢? 既然window.prin...
    99+
    2022-11-13
    ASP.NET MVC 打印表格
  • java打印表格将ResultSet中的数据打印成表格问题
    目录问题描述思路实现测试运行结果总结问题描述 MySQL的查询语句输出如下: mysql> select * from instructor;+-------+---...
    99+
    2022-12-26
    java打印表格 ResultSet数据打印表格 java ResultSet打印表格
  • 怎么使用Python打印漂亮的表格
    本篇内容主要讲解“怎么使用Python打印漂亮的表格”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么使用Python打印漂亮的表格”吧!第一种:使用 format先来看几个小 demo左对齐&...
    99+
    2023-06-15
  • C#实现EPL II格式打印与打印测试
    一、EPL II 格式及打印测试 注意N命令前的换行和最后P1后的换行。将此段代码复制到windows记事本里另存为Print.ext,文件名随便,后缀为ext。然后通过cmd控制命...
    99+
    2024-04-02
  • python 中怎么使用printTable()函数表格打印
    这期内容当中小编将会给大家带来有关python 中怎么使用printTable()函数表格打印,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。编写一个名为printTable()的函数,它接受字符串的列表的...
    99+
    2023-06-02
  • python 打印99乘法表
    #! /usr/bin/pythonfor i in range(1,10): for j in range(1,i+1): print j,'x',i,'=',i*j, print "\n" ...
    99+
    2023-01-31
    乘法表 python
  • php二维数组实现表格打印
    在Web开发中,表格是常见的数据展示形式。而PHP作为一种广泛应用于Web开发的语言,其二维数组提供了便利的方式来创建和展示表格。本文将介绍如何使用PHP的二维数组来实现表格打印。一、什么是PHP二维数组在PHP中,数组是一种非常重要的数据...
    99+
    2023-05-23
  • windows中bartender如何制作表格打印
    这篇“windows中bartender如何制作表格打印”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起...
    99+
    2022-12-02
    windows bartender
  • Python打印九九乘法表
    py代码: i = 1while i <=9:j = 1while j <=i:print("%s%s=%s\t "%(j,i,ji),end="")j += 1print(" ")i +=1...
    99+
    2023-01-31
    九九 乘法表 Python
  • 调试JavaScript代码如何以表格的方式打印对象
    这篇文章主要介绍调试JavaScript代码如何以表格的方式打印对象,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!以表格的方式打印对象下面是一个对象,可以通过 console.table( obj ) 来打印这个对象...
    99+
    2023-06-27
  • Android打印机--小票打印格式及模板设置实例代码
    小票打印就是向打印设备发送控制打印格式的指令集,而这些打印格式需要去查询对应打印机的API文档,这里我把常用的api给封装了一下 文字对齐方式 打印字体大小 字体是...
    99+
    2022-06-06
    模板 Android
  • python之打印九九乘法表
     配置环境:python 3.6 python编辑器:pycharm 整理成代码如下: #!/usr/bin/env python #-*- coding: utf-8 -*- #九九乘法表 #分析:九九乘法表排列呈现的是一个边长为九...
    99+
    2023-01-31
    九九 乘法表 python
  • 云服务器连接打印机:简单又高效的打印方式
    1. 了解云服务器和打印机的基本原理 在开始连接云服务器和打印机之前,我们需要了解一些基本原理。云服务器是一种虚拟化的计算资源,可以通过互联网进行远程访问和管理。而打印机则是一种用于输出纸质文件的设备。 2. 使用网络打印功能连接云服务器...
    99+
    2023-10-27
    高效 打印机 简单
  • Office办公Excel表格打印技巧有哪些
    今天给大家介绍一下Office办公Excel表格打印技巧有哪些。文章的内容小编觉得不错,现在给大家分享一下,觉得有需要的朋友可以了解一下,希望对大家有所帮助,下面跟着小编的思路一起来阅读吧。Office办公软件:Excel表格打印 你曾遇到...
    99+
    2023-06-05
  • wps怎么打印表格在一张A4纸上
    本文小编为大家详细介绍“wps怎么打印表格在一张A4纸上”,内容详细,步骤清晰,细节处理妥当,希望这篇“wps怎么打印表格在一张A4纸上”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。wps打印表格在一张A4纸上&...
    99+
    2023-07-02
  • Excel电子表格打印区域怎么设置
    今天小编给大家分享一下Excel电子表格打印区域怎么设置的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。电子表格打印区域设置方...
    99+
    2023-07-02
  • Python不同格式打印九九乘法的方法是什么
    这篇文章主要介绍“Python不同格式打印九九乘法的方法是什么”,在日常操作中,相信很多人在Python不同格式打印九九乘法的方法是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Python不同格式打印九...
    99+
    2023-06-21
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作