Python Forum

Full Version: python display with '\\' when prints with key-value in dictionary
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
so how do we this with pprint.pprint()? as it gives more formated with proper indent and dictionary display with proper well readable format all its sub dictionary too.
Use yaml if that extra \ is annoying as it should not be for any Python developers,maybe showing the raw dictionary for end-users it could be Undecided
my_dict = {
    "name": "\python",
    "brand": "\Ford",
    "model": "Mustang",
    "year": 1964
}
>>> import yaml
>>> 
>>> print(yaml.dump(my_dict))
{brand: \Ford, model: Mustang, name: \python, year: 1964}

>>> print(yaml.dump(my_dict, width=5))
{brand: \Ford,
  model: Mustang,
  name: \python,
  year: 1964}

>>> print(yaml.dump(my_dict, default_flow_style=False))
brand: \Ford
model: Mustang
name: \python
year: 1964
Excellent.!!!!

will definitely use this yaml format, and let me try to explore on this too. Just a curiosity with this possible to print list values in the format of [item1, item2] with comma separated instead of the yaml format of -item1 in each line.

Thanks a lot, by the way the above solution is an excellent choice too.

Regards,
Maiya
Pages: 1 2