是否有(Unix)shell脚本以人类可读的形式格式化JSON?
基本上,我希望它改变以下内容:
{ "foo": "lorem", "bar": "ipsum" }
Run Code Online (Sandbox Code Playgroud)
...进入这样的事情:
{
"foo": "lorem",
"bar": "ipsum"
}
Run Code Online (Sandbox Code Playgroud) 如何以易于阅读(针对人类读者)格式显示JSON?我主要是寻找缩进和空白,甚至可能是颜色/字体样式等.
如何使Python类可序列化?
一个简单的课程:
class FileItem:
def __init__(self, fname):
self.fname = fname
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能得到输出:
>>> import json
>>> my_file = FileItem('/foo/bar')
>>> json.dumps(my_file)
TypeError: Object of type 'FileItem' is not JSON serializable
Run Code Online (Sandbox Code Playgroud)
没有错误(__CODE__)
如何在Python中打印深度为~4的字典呢?我尝试使用漂亮的打印pprint(),但它不起作用:
import pprint
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(mydict)
Run Code Online (Sandbox Code Playgroud)
我只想"\t"为每个嵌套设置一个缩进(),这样我得到这样的东西:
key1
value1
value2
key2
value1
value2
Run Code Online (Sandbox Code Playgroud)
等等
我怎样才能做到这一点?
有没有上色的输出的方法cat,该方法grep一样.
因为grep,在大多数控制台中,它会显示一个彩色输出,突出显示搜索到的关键字.否则,您可以通过调用来强制它grep --color
是否有一种通用方法根据您的个人选择为任何程序的输出着色.
据我所知,程序本身不对颜色负责.这是壳.
我在FreeBSD 5.2.1中使用默认的shell,看起来它自从epoch以来从未见过颜色.
类的项目涉及解析Twitter JSON数据.我正在获取数据并将其设置到文件中没有太多麻烦,但它只是一行.这对我正在尝试的数据操作很好,但是文件非常难以阅读,我无法很好地检查它,使得为数据操作部分编写代码非常困难.
有没有人知道如何从Python中做到这一点(即不使用命令行工具,我无法工作)?到目前为止,这是我的代码:
header, output = client.request(twitterRequest, method="GET", body=None,
headers=None, force_auth_header=True)
# now write output to a file
twitterDataFile = open("twitterData.json", "wb")
# magic happens here to make it pretty-printed
twitterDataFile.write(output)
twitterDataFile.close()
Run Code Online (Sandbox Code Playgroud)
注意我很感激有人指着我简单的文档等等,但正如我所说,我已经看过并继续需要帮助.一个真正有用的回复将比那里的例子更详细和更具说明性.谢谢
另外: 在Windows命令行中尝试此操作:
more twitterData.json | python -mjson.tool > twitterData-pretty.json
Run Code Online (Sandbox Code Playgroud)
结果如下:
Invalid control character at: line 1 column 65535 (char 65535)
Run Code Online (Sandbox Code Playgroud)
我会给你我正在使用的数据,但它非常大,你已经看到了我用来制作文件的代码.
是否有一种现有的方法可以让json.dumps()输出在ipython笔记本中显示为"漂亮"格式的JSON?
在python中,如果我有一个JSON对象obj,那么我可以
print json.dumps(obj, sort_keys=True, indent=4)
Run Code Online (Sandbox Code Playgroud)
为了获得对象的漂亮打印输出.是否有可能进一步美化输出:特别添加一些颜色?像[1]的结果
cat foo.json | jq '.'
Run Code Online (Sandbox Code Playgroud)
[1] jqJSON Swiss Army工具箱:http://stedolan.github.io/jq/
如果我使用打印字典pprint,它总是在单引号(')周围包装字符串:
>>> from pprint import pprint
>>> pprint({'AAA': 1, 'BBB': 2, 'CCC': 3})
{'AAA': 1, 'BBB': 2, 'CCC': 3}
Run Code Online (Sandbox Code Playgroud)
有没有办法告诉pprint使用双引号(")?我想有以下行为:
>>> from pprint import pprint
>>> pprint({'AAA': 1, 'BBB': 2, 'CCC': 3})
{"AAA": 1, "BBB": 2, "CCC": 3}
Run Code Online (Sandbox Code Playgroud) 我已按照 Google 张量2张量存储库的建议遵循翻译 colab 笔记本教程
导出模型并将其上传到 Google 的 AI Platform 引擎进行在线预测后,我在向模型发出请求时遇到了问题。
我相信翻译模型的输入是源文本的张量。但我收到一个错误TypeError: Object of type 'EagerTensor' is not JSON serializable
def encode(input_str, output_str=None):
"""Input str to features dict, ready for inference"""
inputs = encoders["inputs"].encode(input_str) + [1] # add EOS id
batch_inputs = tf.reshape(inputs, [1, -1, 1]) # Make it 3D.
return {"inputs": batch_inputs}
enfr_problem = problems.problem(PROBLEM)
encoders = enfr_problem.feature_encoders(DATA_DIR)
encoded_inputs = encode("Some text")
model_output = predict_json('project_name','model_name', encoded_inputs,'version_1')["outputs"]
Run Code Online (Sandbox Code Playgroud)
我尝试将张量转换为 numpy 但仍然没有成功。有人能指出我正确的方向吗?
python ×7
json ×6
pretty-print ×4
unix ×2
color-scheme ×1
command-line ×1
dictionary ×1
format ×1
javascript ×1
pprint ×1
tensorflow ×1
twitter ×1