在python中使用argparse解析整个JSON [英] Using argparse in python to parse an entire JSON

查看:37
本文介绍了在python中使用argparse解析整个JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ARGPARSE 库在一个简单的参数中解析整个 Json,问题是它在遇到儿子内部的不同元素时突然停止,例如-"和".

I am trying to parse an entire Json in a simple argument using ARGPARSE library, the thing is that it stops suddenly when it hits different elements inside the son, like "-" and " ".

测试代码如下:

#parse.py
import argparse
parser = argparse.ArgumentParser(description='JSON_test')
parser.add_argument('-contenido', action='store', dest='CONTENIDO',
                    help='JSON content')
args = parser.parse_args()
C = args.CONTENIDO
print (C)

这是运行代码的示例

python parse.py -contenido """{"desde": "2020-03-01","hasta": "2020-03-31","plantas": [{"id": 6,"nombre": "MDS","inversores": [{"id": "Ingeteam 0237","energa": 2152.8070,"pr": 61.5299,"disponibilidad": 81.1770,"factorPlanta": 15.5313}, {"id": "Ingeteam  0538","energa": 2167.5898,"pr": 61.9315,"disponibilidad": 81.0459,"factorPlanta": 15.6381}, {"id": "Ingeteam 0236","energa": 2168.1885,"pr": 61.9511,"disponibilidad": 80.9856,"factorPlanta": 15.6426}, {"id": "Ingeteam 0563","energa": 2206.8702,"pr": 63.0825,"disponibilidad": 80.9455,"factorPlanta": 15.9219}]"""

最后是错误

parse.py: error: unrecognized arguments: 0237,energia: 2152.8070,pr: 61.5299,disponibilidad: 81.1770,factorPlanta: 15.5313}, {id: Ingeteam 0538,energia: 2167.5898,pr: 61.9315,disponibilidad: 81.0459,factorPlanta: 15.6381}, {id: Ingeteam 0236,energia: 2168.1885,pr: 61.9511,disponibilidad: 80.9856,factorPlanta: 15.6426}, {id: Ingeteam 0563,energia: 2206.8702,pr: 63.0825,disponibilidad: 80.9455,factorPlanta: 15.9219}]"

我们的架构不允许我们使用文件来解析,因此解决方法将不起作用:(.我能做什么?我已经阅读了很多 SOF 帖子,明天我将对其进行测试,但我认为它们不适合这个特定问题,我们的 json 非常大,我们需要从单个参数运行它.提前致谢!

Our architecture wont let us use a file to parse, so that work around won't work :(. What can i do? i've read a lot of SOF posts and i will test them tomorrow but i am thinking that they won't suit this specific problem, our json is very large and we need to run it from a single argument. Thanks in advance!

推荐答案

使用一个单引号而不是三个双引号来包装命令行参数.三重双引号是 Python 语法,而不是 shell 语法.

Use one single-quote to wrap the command line argument rather than triple-double-quotes. The triple-double-quotes are a Python syntax, not a shell syntax.

python parse.py -contenido '{"desde": "2020-03-01","hasta": "2020-03-31","plantas": [{"id": 6,"nombre": "MDS","inversores": [{"id": "Ingeteam 0237","energa": 2152.8070,"pr": 61.5299,"disponibilidad": 81.1770,"factorPlanta": 15.5313}, {"id": "Ingeteam  0538","energa": 2167.5898,"pr": 61.9315,"disponibilidad": 81.0459,"factorPlanta": 15.6381}, {"id": "Ingeteam 0236","energa": 2168.1885,"pr": 61.9511,"disponibilidad": 80.9856,"factorPlanta": 15.6426}, {"id": "Ingeteam 0563","energa": 2206.8702,"pr": 63.0825,"disponibilidad": 80.9455,"factorPlanta": 15.9219}]'

您也可以使用一个双引号将参数括起来,但您需要对参数中的每个双引号进行转义.

You may also use one double-quote to wrap the argument, but you'll need to escape each double-quote in the argument.

这篇关于在python中使用argparse解析整个JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆