变量在打印时更改其格式(Variable changes its format on printing)

我试图使用Python将带有键值数据的变量存储在一个文件中,但是当我尝试打印它时,它会以不同的格式出现。

我希望结果像这样打印 -

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" } }

这是我在打印数据时得到的输出 -

{'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'}

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'}

最满意答案

您没有说明打印时对您重要的内容,也没有说明您当前是否正在尝试打印。

字典中没有格式。 代码中的任何格式化只是为了使代码看起来像人类可读,并且实际上并未存储在data字典中(只保留每个字符串元素中的格式,即在一对引号之间)。

如果它只是您关注的格式(多行和缩进),解决这个问题的最简单方法是使用漂亮打印模块或JSON模块 - 要么应该完成这项工作,这取决于您对工作方式的偏好希望数据看起来以及您希望对打印输出格式有多少控制。 特别是,JSON输出占据了更多的垂直屏幕空间,但有些人可能会认为它略微更具人性化。

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转储:

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" } >>>

如果您担心项目的打印顺序,那么您需要有一个数组,按照首选顺序存储密钥(字典没有任何不必要的顺序),然​​后迭代您的密钥和逐个手动打印字典项(可能使用键阵列上的列表解析)。

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).

更多推荐