python中argsparse_在python中使⽤argparse解析整个JSON 我试图使⽤ARGPARSE库在⼀个简单的参数中解析整个Json,但事实是,当它碰到⼦内部的不同元素(例如“-”和“”)时,它会突然停⽌。
这是测试代码:
#parse.py
import argparse
javabean为什么添加外部属性
parser = argparse.ArgumentParser(description='JSON_test')
parser.add_argument('-contenido', action='store', dest='CONTENIDO',
help='JSON content')
c语言extern的用法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
计算机程序设计大赛
python请求并解析json数据0563,energia: 2206.8702,pr: 63.0825,disponibilidad: 80.9455,factorPlanta: 15.9219}]"
我们的体系结构不允许我们使⽤⽂件进⾏解析,因此⽆法解决:(。我该怎么办?我读了很多SOF帖⼦,明天我将对其进⾏测试,但我认为它们不会起作⽤。为了适应此特定问题,我们的json⾮常⼤,我们需要从单个参数运⾏它。
解决⽅案
indexofarielrebel使⽤⼀个单引号引起来的命令⾏参数,⽽不是三重双引号。三重双引号是Python语法,⽽不是Shell语法。
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}]'
您还可以使⽤⼀个双引号将参数引起来,但是您需要对参数中的每个双引号进⾏转义。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。