从python中无法打印出json ?

时间:2023-01-24 21:43:46

Whenever I try to print out json from python, it ignores line breaks and prints the literal string "\n" instead of new line characters.

每当我尝试从python中打印json时,它忽略了换行符,并输出了字符串“\n”,而不是新的行字符。

I'm generating json using jinja2. Here's my code:

我使用jinja2生成json。这是我的代码:

print json.dumps(template.render(**self.config['templates'][name]))

It prints out everything in the block below (literally - even the quotes and "\n" strings):

它打印出下面的所有东西(字面上——甚至是引号和“\n”字符串):

"{\n    \"AWSTemplateFormatVersion\" : \"2010-09-09\",\n    \"Description\" : ... 

(truncated)

(截断)

I get something like this whenever I try to dump anything but a dict. Even if I try json.loads() then dump it again I get garbage. It just strips out all line breaks.

每当我试图转储任何东西时,我都会得到这样的东西,即使我尝试了json.load(),然后再把它转储出来,我就会得到垃圾。它只是去掉了所有的换行符。

What's going wrong?

什么错了吗?

4 个解决方案

#1


17  

This is what I use for pretty-printing json-objects:

这是我用于打印json对象的方法:

def get_pretty_print(json_object):
    return json.dumps(json_object, sort_keys=True, indent=4, separators=(',', ': '))

print get_pretty_print(my_json_obj)

json.dumps() also accepts parameters for encoding, if you need non-ascii support.

dump()也接受编码的参数,如果您需要非ascii支持。

#2


7  

json.dumps() returns a JSON-encoded string. The JSON standard mandates that newlines are encoded as \\n, which is then printed as \n:

转储()返回json编码的字符串。JSON标准要求将新行编码为\\n,然后打印为\n:

>>> s="""hello
... there"""
>>> s
'hello\nthere'
>>> json.dumps(s)
'"hello\\nthere"'
>>> print(json.dumps(s))
"hello\nthere"

There's not much you can do to change that if you want to keep a valid JSON string. If you want to print it, the correct way would be to print the JSON object, not its string representation:

如果您想要保留有效的JSON字符串,那么您就不能做太多更改。如果您想要打印它,正确的方法是打印JSON对象,而不是它的字符串表示:

>>> print(s)
hello
there
>>> print(json.loads(json.dumps(s)))  # pointless; just for demonstration...
hello
there

#3


0  

If your string is already JSON then pretty print it using

如果您的字符串已经是JSON,那么就用它来打印。

def pp_json(json_string):
# converts json to dict then back to string... ridiculous but not pointless 
    print(json.dumps(json.loads(json_string), sort_keys=True, indent=4)) 
    return

pp_json(your_json_string)

#4


0  

The the problem is that your input to json.dumps is a string. Try the following:

问题是您的输入是json。转储是一个字符串。试试以下:

print type(template.render(**self.config['templates'][name]))

It you are doing this to indent etc... try the following:

你这样做是为了缩进等等…试试以下:

print json.dumps(json.loads(template.render(**self.config['templates'][name])), sort_keys=True, indent=4)

#1


17  

This is what I use for pretty-printing json-objects:

这是我用于打印json对象的方法:

def get_pretty_print(json_object):
    return json.dumps(json_object, sort_keys=True, indent=4, separators=(',', ': '))

print get_pretty_print(my_json_obj)

json.dumps() also accepts parameters for encoding, if you need non-ascii support.

dump()也接受编码的参数,如果您需要非ascii支持。

#2


7  

json.dumps() returns a JSON-encoded string. The JSON standard mandates that newlines are encoded as \\n, which is then printed as \n:

转储()返回json编码的字符串。JSON标准要求将新行编码为\\n,然后打印为\n:

>>> s="""hello
... there"""
>>> s
'hello\nthere'
>>> json.dumps(s)
'"hello\\nthere"'
>>> print(json.dumps(s))
"hello\nthere"

There's not much you can do to change that if you want to keep a valid JSON string. If you want to print it, the correct way would be to print the JSON object, not its string representation:

如果您想要保留有效的JSON字符串,那么您就不能做太多更改。如果您想要打印它,正确的方法是打印JSON对象,而不是它的字符串表示:

>>> print(s)
hello
there
>>> print(json.loads(json.dumps(s)))  # pointless; just for demonstration...
hello
there

#3


0  

If your string is already JSON then pretty print it using

如果您的字符串已经是JSON,那么就用它来打印。

def pp_json(json_string):
# converts json to dict then back to string... ridiculous but not pointless 
    print(json.dumps(json.loads(json_string), sort_keys=True, indent=4)) 
    return

pp_json(your_json_string)

#4


0  

The the problem is that your input to json.dumps is a string. Try the following:

问题是您的输入是json。转储是一个字符串。试试以下:

print type(template.render(**self.config['templates'][name]))

It you are doing this to indent etc... try the following:

你这样做是为了缩进等等…试试以下:

print json.dumps(json.loads(template.render(**self.config['templates'][name])), sort_keys=True, indent=4)