Comments (8)
That make sense. I could imagine few other solutions:
- Add a new property custom_format which for each field a
callable[[str, Any], str]
. This would allow the consumer of PrettyTable to use any custom format they want. I like this version because you could do something like that
>>> "{:,} USD".format(12344556.45)
'12,344,556.45 USD'
- Add a flag with support python2 format, which could be used for few version the time everyone can migrate to the new format.
Do you think any of them could work ?
from prettytable.
-
Yep, this could work, like a general purpose formatter suggested above (#99 (comment))
-
Python 2 is nearly two years past EOL, we're not adding back support at this stage :)
from prettytable.
https://github.com/jazzband/prettytable#style-options says:
float_format
- A string which controls the way floating point data is printed. This works like:print("%<float_format>f" % data)
None of the examples would work with plain percent formatting:
>>> print("%0.2f" % 0.0125)
0.01
>>> print("%0.2" % 0.0125)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: incomplete format
>>> print("0.2%f" % 0.0125)
0.20.012500
>>> print("0.2%" % 0.0125)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: incomplete format
This looks closest? Note the input value is also changed:
>>> print("%0.2f%%" % (0.0125*100))
1.25%
But note float_format
provides the leading %
and trailing f
already, you can't also supply the %%
at the very end. Which is to say percentage formatting isn't currently supported.
I suggest you pre-process your data into the required 1,25%
form.
from prettytable.
Thanks you hugovk!
%0.2f%%
is what I was looking for. I thought there should be an elegant way to achieve this output with PrettyTable. Sadly it's apparently not. So, I'll follow your suggestion and format my float numbers where needed into string with % and then add it to PrettyTable.
float_format
worthless in this case
It would be useful to support percentage formatting for float numbers imho
from prettytable.
I'd be open to a PR to add support for that, either percent_format
or perhaps a general purpose formatter would be more flexible?
from prettytable.
@hugovk ,
I looked at the code, and I see you are applying the format like that
def _format_value(self, field, value):
if isinstance(value, int) and field in self._int_format:
value = ("%%%sd" % self._int_format[field]) % value
elif isinstance(value, float) and field in self._float_format:
value = ("%%%sf" % self._float_format[field]) % value
return str(value)
did you consider using something more like that self._float_format[field].format(value)
where you let the consumer defining exactly what format to apply. For example for percent you could use "{:,.2%}
and thousand and decimal {:,.2}
If you are happy to use .format
I will prepare a PR for you
from prettytable.
Thanks for the suggestion, my main concern would be not wanting to break backward compatibility for people using the existing formatting; this is a 12 year old library with nearly 4m downloads per month.
from prettytable.
@MicroElf Please can you have a look at PR #136 and see if it would address your needs?
from prettytable.
Related Issues (20)
- Support for rich colored cell data HOT 2
- Support displaying selected fields for CSV format
- Pretty printing inside cloudwatch logs HOT 1
- Is max_width word-wrapping configurable? HOT 10
- PrettyTable.add_rows method support divider Argument
- Calculate column width HOT 4
- Full-width characters cannot be wrapped correctly. HOT 2
- can you add an option for the divider, so insted of add the divider below it add its above?
- Negative Integer and Float in Red and positive in Green
- argument max_width doesn't work
- 3.10.0: pytest fails because incorrect module name HOT 2
- max_table_width does not work as expected HOT 7
- Colortable with title leads to broken layout HOT 6
- Add color to field names HOT 4
- Print table failed when I created a copy.py in the same directory with my other python files. HOT 1
- Markdown tables with narrow columns don't render
- Mediawiki Output
- Cannot release from Jazzband to PyPI HOT 3
- `float_format` not working HOT 1
- `float_format` not working HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from prettytable.