Python Forum
python display with '\\' when prints with key-value in dictionary - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: python display with '\\' when prints with key-value in dictionary (/thread-26109.html)

Pages: 1 2


RE: python display with '\\' when prints with key-value in dictionary - maiya - Apr-27-2020

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.


RE: python display with '\\' when prints with key-value in dictionary - snippsat - Apr-27-2020

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



RE: python display with '\\' when prints with key-value in dictionary - maiya - May-30-2020

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