生成ascii艺术表的实用程序

yatg的Python项目详细描述


yatg(另一个表生成器)是一个用于生成ascii艺术表的实用程序。

Build status

简介

此工具可以将csv或html转换为ascii艺术表。

输出示例(Emacs样式):

+---------+-----------------+----------+
|         | Average         | Red eyes |
|         +--------+--------+          |
|         | height | weight |          |
+---------+--------+--------+----------+
| Males   | 1.9    | 0.003  | 40%      |
+---------+--------+--------+----------+
| Females | 1.7    | 0.002  | 43%      |
+---------+--------+--------+----------+

输出示例(orgmode样式):

| Header content 1 | Header content 2 |
|------------------+------------------|
| Body content 1   | Body content 2   |
| Body content 3   | Body content 4   |
| Body content 5   | Body content 6   |

输出示例(mysql样式):

+------------------+------------------+
| Header content 1 | Header content 2 |
+------------------+------------------+
| Body content 1   | Body content 2   |
| Body content 3   | Body content 4   |
| Body content 5   | Body content 6   |
+------------------+------------------+

输出示例(标记样式):

| Header content 1 | Header content 2 |
|------------------|------------------|
| Body content 1   | Body content 2   |
| Body content 3   | Body content 4   |
| Body content 5   | Body content 6   |

安装

从pypi安装yatg:

$ pip install yatg

或来自github:

$ curl -O https://raw.githubusercontent.com/10gic/yatg/master/yatg/yatg.py && chmod u+x yatg.py

用法

作为命令行工具

选项:

usage: yatg [-h] [-i INFILE] [-f FORMAT] [-d DELIMITER] [-o OUTFILE]
            [-s STYLE] [--no-header] [--column-align ALIGN]
            [--width1-chars CHARS] [--align-in-tty]

Yet Another Table Generator, convert CSV or html table to ASCII art table.

optional arguments:
  -h, --help            show this help message and exit
  -i INFILE, --input-file INFILE
                        source file, read from stdin if not specified
  -f FORMAT, --input-format FORMAT
                        format of input file, can be 'html' or 'csv', auto
                        guess it if not specified
  -d DELIMITER, --csv-delimiter DELIMITER
                        delimiter of csv data, guess it if not specified
  -o OUTFILE, --output-file OUTFILE
                        output file, write to stdout if not specified
  -s STYLE, --output-style STYLE
                        specify output table style, support 'orgmode',
                        'emacs', 'mysql', 'markdown', default is orgmode style
  --no-header           horizontal header line would not be printed if this
                        option present
  --column-align ALIGN  specify align string of columns, support 'l/r'. For
                        example, 'llrr' specify first two colums align left,
                        3rd and 4th columns align right. Default alignment is
                        left.
  --width1-chars CHARS  specify chars that should consider one character width
                        by force, only 'emoji' is supported currently. This
                        option requires package emoji.
  --align-in-tty        set column aligned in tty. This option requires
                        package blessed. If this option present, option
                        --width1-chars would be ignored. NOTE: (1) this option
                        requires you in a tty, (2) each column width must less
                        than width of tty, please enlarge your tty window if
                        you have long cell data.

作为图书馆

示例:

>>> import yatg
>>> print(yatg.csv_2_ascii_table([["head1", "head2"],
... ["content1", "content2"],
... ["content3", "content4"]]))
| head1    | head2    |
|----------+----------|
| content1 | content2 |
| content3 | content4 |

>>> print(yatg.html_2_ascii_table("""
... <table border="1">
...     <tr>
...         <td>1st row</td>
...         <td colspan=2>colspan2</td>
...         <td rowspan=2>rowspan2</td>
...     </tr>
...     <tr>
...         <td>2nd row</td>
...         <td>under colspan2</td>
...         <td>under colspan2</td>
...     </tr>
...     <tr>
...         <td>3rd row</td>
...         <td colspan=3>colspan3</td>
...     </tr>
... </table>""", output_style='emacs'))
+---------+---------------------------------+----------+
| 1st row | colspan2                        | rowspan2 |
+---------+----------------+----------------+          |
| 2nd row | under colspan2 | under colspan2 |          |
+---------+----------------+----------------+----------+
| 3rd row | colspan3                                   |
+---------+--------------------------------------------+

功能文档:

>>> print(yatg.csv_2_ascii_table.__doc__)
 Convert csv to ascii table.

    Arguments:
      csv_content: Data of input csv, can be string or 'list of list'.
      csv_delimiter: The delimiter of csv string data (default is ',').
      output_style: The output style: emacs|orgmode|mysql|markdown
                    (default is 'orgmode').
      column_align: align string of columns, support 'l/r'. For example,
                   'llrr' specify first two colums align left, 3rd and 4th
                   columns align right. Default alignment is left.
      no_header: whether print horizontal header line. Default is False
      align_in_tty: force align column in tty

    Returns:
      Ascii table

>>> print(yatg.html_2_ascii_table.__doc__)
 Convert html table to ascii table.

    Arguments:
      html_content: Data of input html.
      output_style: The output style: emacs|orgmode|mysql|markdown
                    (default is 'orgmode').
      column_align: align string of columns, support 'l/r'. For example,
                   'llrr' specify first two colums align left, 3rd and 4th
                   columns align right. Default alignment is left.
      no_header: whether print horizontal header line. Default is False
      align_in_tty: force align column in tty

    Returns:
      Ascii table

功能

  • 支持colspan和rowspan。
  • 支持多种输出样式:emacs/orgmode(默认)/mysql/markdown样式。
  • 当单元格同时包含ascii和非ascii字符时,表保持对齐。
  • 支持自定义列对齐。
  • 标题行是可选的。
  • 与Python2和Python3兼容。
  • 主要功能没有第三部分依赖关系。

限制

  • 一个表格单元格中的多行文本将变为一行。
  • 不支持嵌套表。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java连接usb到uart设备到安卓设备>3.1   可以强制Php中的web应用程序与Java中的桌面应用程序一起工作吗?   java为什么自定义系统类加载器不工作?   数组在Java中解析具有多个分隔符的字符串   PMD Java 8德米特定律   JavaSpringMVC表单验证不适用于嵌套的复杂类型   让Eclipse Java组织导入以使用Google checkstyle   java Appium:无法创建新会话   java如何在数组中声明新字段   java如何解决“无法初始化类org.apache.cassandra.config.DatabaseDescriptor”?   java AsyncTask创建socket   java向@CreatedBy添加更多信息   如何在ubuntu中运行包含大量jars依赖项的java文件   java如何使用<s:select>标记并在中休眠来填充下拉列表?   java获取错误:找不到符号变量“level”和“next_level_button”   javaweb应用中基于UI的ajax显示代码流   Java长到MySql   java JvisualVM:奇怪的应用程序行为   ubuntu将Java程序的输出结果保存到一个文件中