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
610 stars 43 forks source link

Get string output for markdown writer without printing to stdout #3

Closed nettrino closed 6 years ago

nettrino commented 6 years ago

Hello,

I am trying to get the raw string output from a MarkdownTableWriter without printing to stdout. I have also tried redirecting output using the with io.StringIO() as buf, redirect_stdout(buf): pattern but that was unsuccessful. Trying to write to a file using the example of the Readme leads to AttributeError: 'MarkdownTableWriter' object has no attribute 'open'. Any hints on how to resolve this? Essentially I just want a variable to hold all the contents of writer.write_table()

nettrino commented 6 years ago

Closing this as I resolved it using the configure_stream.py example. It would be useful if this was part of the README. It would be also an option that the default output is not stdout but that write_table() returns a string/iterator, that users can redirect as they wish.

thombashi commented 6 years ago

@nettrino Thank you for your feedback.

I had added dumps method at pytablewriter 0.32.0 that can get write_table() output as an str, and added a description to the README. I would rather stay the default output of write_table(), for backward compatibility.