How to beautify python data?
Python Formatter helps to format unformatted or ugly Python data and helps to save and share Python. Show
What can you do with Python Formatter?
Example of PythonPython data Try it. HealthInsurance: def __init__(self, name, founded): self.name = name self.founded = founded healthInsurance = HealthInsurance("UnitedHealth", 1977) print(healthInsurance.name) print(healthInsurance.founded) For Advanced UsersPython External URLLoad Python External URL in Browser URL like this https://codebeautify.org/python-formatter-beautifier?url=external-url https://codebeautify.org/python-formatter-beautifier?url=https://gist.githubusercontent.com/cbmgit/81747051958c0494a143498238570061/raw/HealthInsurance.py Python Data as ParameterLoad Python Data in Browser URL input like this https://codebeautify.org/python-formatter-beautifier?input=inputdata https://codebeautify.org/python-formatter-beautifier?input=print("Hello,World!") Dealing with data is essential for any Pythonista, but sometimes that data is just not very pretty. Computers don’t care about formatting, but without good formatting, humans may find something hard to read. The output isn’t pretty when you use The By the end of this tutorial, you’ll:
Along the way, you’ll also see an HTTP request to a public API and JSON parsing in action. Understanding the Need for Python’s Pretty PrintThe Python Before you
start exploring >>>
Here, you make a basic >>>
Oh dear! One huge line with no newlines. Depending on your console settings, this might appear as one very long line. Alternatively, your console output might have its word-wrapping mode on, which is the most common situation. Unfortunately, that doesn’t make the output much friendlier! If you look at the first and last characters, you can see that this appears to be a list. You might be tempted to start writing a loop to print the items:
This Sure, you could write a function that uses recursion to find a way to print everything. Unfortunately, you’ll likely run into some edge cases where this won’t work. You might even find yourself writing a whole module of functions just to get to grips with the structure of the data! Enter the Working With pprint
>>>
Then, instead of going with the normal
This function prints >>>
How pretty! The keys of the dictionaries are even visually indented! This output makes it so much more straightforward to scan and visually analyze data structures. If you’re a fan of typing as
little as possible, then you’ll be pleased to know that >>>
However, even the default output may be too much information to scan at first. Maybe all you really want is to verify that you’re dealing with a list of plain objects. For that, you’ll want to tweak the output a little. For
these situations, there are various parameters you can pass to Exploring Optional Parameters of pprint()In this section, you’ll learn about all the parameters available for Summarizing Your Data: depthOne of the handiest parameters to play around with is >>>
Now you can immediately see that this is indeed a list of dictionaries. To explore the data structure further, you can increase the depth by one level, which will print all the top-level keys of the
dictionaries in >>>
Now you can quickly check whether all the dictionaries share their top-level keys. This is a valuable observation to make, especially if you’re tasked with developing an application that consumes data like this. Giving Your Data Space: indentThe >>>
The most important part of the indenting behavior of
Since there’s no nesting in the examples above, the amount of indentation is based completely on the When there is nesting, however, the indentation is applied to the first element in-line, and >>>
This is just another part of the pretty in Python’s Limiting Your Line Lengths: widthBy default, >>>
When you leave the width at the default of eighty characters, the dictionary at However, the dictionary at >>>
If you set the width to a large value like >>>
Here, you get the effects of setting >>>
It’s hard to get Python’s In this example, on top of learning about Squeezing Your Long Sequences: compactYou might think that If >>>
Pretty-printing this list
using the default settings prints out the abbreviated version on one line. Limiting
Directing Your Output: streamThe >>>
Here you create a file object with Python does have its own logging module. However, you can also use Preventing Dictionary Sorting: sort_dictsAlthough dictionaries are generally considered unordered data structures, since Python 3.6, dictionaries are ordered by insertion.
>>>
Unless you set When Prettifying Your Numbers: underscore_numbersThe >>>
If you tried running this call to If Next, you’ll cover how to create a Creating a Custom PrettyPrinter ObjectIt’s possible to create an instance of >>>
With these commands, you:
Note that
the arguments you passed to This way, you can have various printer presets—perhaps some going to different streams—and call them when you need them. Getting a Pretty String With pformat()What if you don’t want to send the pretty output of Whatever it is that you might want to do with the string pre-output, you can get the string by using >>>
Another use case for this might be if you’re building an API and want to send a pretty string representation of the JSON string. Your end users would probably appreciate it! Handling Recursive Data StructuresPython’s Ask yourself what happens when a recursive function runs into a recursive data structure. Imagine that you have dictionary
If your imaginary recursive function has no way to
handle this circular reference, it’ll never finish printing! It would print Luckily, both the normal >>>
While Python’s regular If you want to explore why this structure is recursive, you can learn more about passing by reference. ConclusionYou’ve explored the primary usage of the In this tutorial, you’ve learned how to:
To help you get to grips with the function and parameters, you used an example of a data structure representing
some users. You also explored some situations where you might use Congratulations! You’re now better equipped to deal with complex data by using Python’s How do you code beautify in Python?You can format Python code by clicking on the button "Format".. Copy and paste your Python string.. Drag and drop your Python file.. Directly type your code in the editor.. Browse your Python file (Clicks on "Browse" button).. Retrieve your Python file from a URL(Clicks on "Load Python from URL" button).. Is there a prettier for Python?and with plugins you can use it for Python, PHP, Swift, Ruby, Java and more. It integrates with the most popular code editors, including VS Code, Sublime Text, Atom and more. Prettier is hugely popular, as in February 2018 it has been downloaded over 3.5 million times.
How do I beautify Python code in Visual Studio?The code formatting is available in Visual Studio Code (VSCode) through the following shortcuts or key combinations: On Windows Shift + Alt + F. On macOS Shift + Option + F. On Linux Ctrl + Shift + I.
What is pretty print in Python?The pprint module in Python is a utility module that you can use to print data structures in a readable, pretty way. It's a part of the standard library that's especially useful for debugging code dealing with API requests, large JSON files, and data in general.
|