thombashi / pytablewriter

pytablewriter is a Python library to write a table in various formats: AsciiDoc / CSV / Elasticsearch / HTML / JavaScript / JSON / LaTeX / LDJSON / LTSV / Markdown / MediaWiki / NumPy / Excel / Pandas / Python / reStructuredText / SQLite / TOML / TSV.
https://pytablewriter.rtfd.io/
MIT License
611 stars 43 forks source link

0.47: quotes added around strings with NumpyTableWriter #25

Closed hugovk closed 4 years ago

hugovk commented 4 years ago

With pytablewriter 0.47, I'm seeing quotes added around some strings with NumpyTableWriter and PandasDataFrameWriter.

Code

import pytablewriter
from pytablewriter import (
    HtmlTableWriter,
    MarkdownTableWriter,
    NumpyTableWriter,
    PandasDataFrameWriter,
    RstSimpleTableWriter,
)

def _tabulate(data, format="markdown"):
    """Return data in specified format"""

    format_writers = {
        "markdown": MarkdownTableWriter,
        "rst": RstSimpleTableWriter,
        "html": HtmlTableWriter,
        "pandas": PandasDataFrameWriter,
        "numpy": NumpyTableWriter,
    }

    writer = format_writers[format]()
    if format != "html":
        writer.margin = 1

    writer.header_list = sorted(set().union(*(d.keys() for d in data)))
    writer.value_matrix = data

    if format == "numpy":
        return writer.tabledata.as_dataframe().values
    elif format == "pandas":
        return writer.tabledata.as_dataframe()
    return writer.dumps()

if __name__ == "__main__":
    print(pytablewriter.__version__)
    data = [{"category": "without_mirrors", "downloads": 4593356}]

    print("numpy:")
    output = _tabulate(data, format="numpy")
    print(output)

    print("pandas:")
    output = _tabulate(data, format="pandas")
    print(output)

Before

$ python --version
Python 3.8.1
$ pip install DataProperty==0.43.3 pytablewriter==0.46.3 tabledata==0.9.1
...
$ p 1.py
0.46.3
numpy:
[['without_mirrors' 4593356]]
pandas:
          category  downloads
0  without_mirrors    4593356

After

$ pip install -U pytablewriter
Collecting pytablewriter
  Using cached pytablewriter-0.47.0-py2.py3-none-any.whl (70 kB)
Collecting DataProperty<2,>=0.45.0
  Using cached DataProperty-0.45.0-py2.py3-none-any.whl (25 kB)
Requirement already satisfied, skipping upgrade: mbstrdecoder<2,>=0.8.3 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from pytablewriter) (0.8.4)
Requirement already satisfied, skipping upgrade: typepy[datetime]<2,>=0.6.4 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from pytablewriter) (0.6.4)
Requirement already satisfied, skipping upgrade: setuptools>=38.3.0 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from pytablewriter) (45.1.0)
Collecting tabledata<2,>=0.9.2
  Using cached tabledata-0.9.2-py2.py3-none-any.whl (11 kB)
Requirement already satisfied, skipping upgrade: pathvalidate<3; python_version >= "3.5" in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from pytablewriter) (2.1.0)
Requirement already satisfied, skipping upgrade: six<2.0.0,>=1.10.0 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from pytablewriter) (1.14.0)
Requirement already satisfied, skipping upgrade: msgfy<1,>=0.0.6 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from pytablewriter) (0.0.7)
Requirement already satisfied, skipping upgrade: chardet<4,>=3.0.4 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from mbstrdecoder<2,>=0.8.3->pytablewriter) (3.0.4)
Requirement already satisfied, skipping upgrade: pytz>=2018.9; extra == "datetime" in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from typepy[datetime]<2,>=0.6.4->pytablewriter) (2019.3)
Requirement already satisfied, skipping upgrade: python-dateutil<3.0.0,>=2.8.0; extra == "datetime" in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from typepy[datetime]<2,>=0.6.4->pytablewriter) (2.8.1)
Installing collected packages: DataProperty, tabledata, pytablewriter
  Attempting uninstall: DataProperty
    Found existing installation: DataProperty 0.43.3
    Uninstalling DataProperty-0.43.3:
      Successfully uninstalled DataProperty-0.43.3
  Attempting uninstall: tabledata
    Found existing installation: tabledata 0.9.1
    Uninstalling tabledata-0.9.1:
      Successfully uninstalled tabledata-0.9.1
  Attempting uninstall: pytablewriter
    Found existing installation: pytablewriter 0.46.3
    Uninstalling pytablewriter-0.46.3:
      Successfully uninstalled pytablewriter-0.46.3
Successfully installed DataProperty-0.45.0 pytablewriter-0.47.0 tabledata-0.9.2
$ p 1.py
0.47.0
numpy:
[['"without_mirrors"' 4593356]]
pandas:
            category  downloads
0  "without_mirrors"    4593356
thombashi commented 4 years ago

@hugovk Thank you for your report.

The problem fixed by the above commit. I will release the next version after several other fixes/enhancements.

hugovk commented 4 years ago

Thank you! I've confirmed installing from master fixes it.

thombashi commented 4 years ago

Now, You can install pytablewriter 0.48.0 from PyPI which includes the commit.

hugovk commented 4 years ago

Thank you, confirmed fixed!