Kunal Sehegal
Kunal Sehegal

Reputation: 63

Variable changes its format on printing

I am trying to store a variable with Key Value data in a file using Python, but when I try printing it, it comes up in a different format.

I want the result to be printed like this-

data={"name":'name',"description": "This is my offering","icon":"/csa/api/blobstore/Magic_RGB_blue_NT.png?tag=library","version": "1.0.0",
                  "design": {
                    "@self": "@self"
                    }
                }

This is the output I get while printing the data-

{'icon': '/csa/api/blobstore/Magic_RGB_blue_NT.png?tag=library', 'design': {'@self': '@self'}, 'name': 'name', 'version': '1.0.0', 'description': 'This is my offering'}

Upvotes: 0

Views: 756

Answers (3)

Son of a Beach
Son of a Beach

Reputation: 1779

You haven't stated what is important to you when printing, nor how you are currently attempting to print.

There is no formatting within a dictionary. Any formatting in your code is merely to make the code look human readable and is not actually stored within your data dictionary (only formatting within each string element is retained, ie, between a pair of quotes).

If it is merely the format (multiple lines and indents) that you are concerned about, the easiest way to resolve that is to use either the Pretty Print module or the JSON module - either should do the job, depending on your preferences for how you want the data to look and how much control you want to have over the printed output format. In particular, the JSON output occupies more vertical screen space, but some people may think that it is marginally more human readable.

PrettyPrint pprint:

import pprint
data={"name":'name',"description": "This is my offering","icon":"/csa/api/blobstore/Magic_RGB_blue_NT.png?tag=library","version": "1.0.0","design": {"@self": "@self"}}
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(data)
>>> 
{   'description': 'This is my offering',
    'design': {   '@self': '@self'},
    'icon': '/csa/api/blobstore/Magic_RGB_blue_NT.png?tag=library',
    'name': 'name',
    'version': '1.0.0'}
>>>

JSON dumps:

import json
data={"name":'name',"description": "This is my offering","icon":"/csa/api/blobstore/Magic_RGB_blue_NT.png?tag=library","version": "1.0.0","design": {"@self": "@self"}}
print(json.dumps(data, indent=4))
>>> 
{
    "icon": "/csa/api/blobstore/Magic_RGB_blue_NT.png?tag=library", 
    "design": {
        "@self": "@self"
    }, 
    "name": "name", 
    "version": "1.0.0", 
    "description": "This is my offering"
}
>>> 

If you are concerned about the the order in which the items are printed, then you'll need to have an array that stores the keys in their preferred order (dictionaries don't have any inherant ordering), and then iterate through your keys and print the dictionary items out manually one by one (perhaps using a list comprehension on your keys array).

Upvotes: 8

Code-Apprentice
Code-Apprentice

Reputation: 83527

Note that a variable doesn't contain any formatting to be changed. The python interpreter reads text from the REPL or a file and turns them into instructions for the CPU of your machine. None of this involves formatting until you call print(). By default, print() calls str() which then provides some very basic formatting. For dictionaries, this includes the curly braces, colons, and commas. If you want anything more than this, you will need to do it yourself. Alternatively, you can find a Python module that helps reduce some of the tedium.

Upvotes: 0

Chris
Chris

Reputation: 22953

Python doesn't respect the indention or newlines you use to define your data structure, and ignores any format you had when print()ing it. You don't have very many options here, but perhaps you can use the json.dumps() function to format your code. The format does not match your expected output exactly, but it comes fairly close:

>>> data = {"name":'name',"description": "This is my offering","icon":"/csa/api/blobstore/Magic_RGB_blue_NT.png?tag=library","version": "1.0.0",
                  "design": {
                    "@self": "@self"
                    }
                }
>>> import json
>>> print(json.dumps(data, indent=2))
{
  "description": "This is my offering",
  "version": "1.0.0",
  "icon": "/csa/api/blobstore/Magic_RGB_blue_NT.png?tag=library",
  "name": "name",
  "design": {
    "@self": "@self"
  }
}
>>>

Upvotes: 1

Related Questions