【问题标题】:Using argparse in python to parse an entire JSON在 python 中使用 argparse 来解析整个 JSON
【发布时间】:2020-07-19 03:56:26
【问题描述】:

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

这里是测试代码:

#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 非常大,我们需要从一个参数运行它。 提前致谢!

【问题讨论】:

  • 看sys.argv[1:]。这就是argparse 所看到的。您的 JSON 在该列表中显示为一个长字符串。需要多少引用将取决于您的操作系统和外壳。
  • 模拟一个参数(-contenido xxxxxxxxx)的CMD内容,使sys返回xxxxxxxxx的命令是什么?
  • 这是它运行的 ubuntu 中的 CMD / powershell 问题。 TY全部

标签: python json parsing arguments argparse


【解决方案1】:

使用一个单引号来包裹命令行参数,而不是三双引号。三双引号是 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}]'

您也可以使用一个双引号来包装参数,但您需要对参数中的每个双引号进行转义。

【讨论】:

  • 用一个单引号 (') 它停在第一个 2020 用单双引号 (") 它停在 "ingeteam " 旁边的 0237 用双双引号 ("") 它再次停在第一个 2020 年,并在 0237 年再次使用三个双引号 (""")。不确定它是如何工作的:/
  • @CristobalRencoret:文本中有单引号吗?
  • @rici 没有,只​​有双 Q ( " )
  • @cristobal:Linux 上的 Bash。因此,您可能在使用 Windows 时遇到了一些奇怪的问题。在你的问题中添加这样的细节总是好的。
  • Single quotes apparently don't work in the Windows shell。此外,如果您使用的是 PowerShell,this answer 中的 run-native 函数可能会很有用。
猜你喜欢
  • 2015-03-04
  • 2018-12-14
  • 1970-01-01
  • 2014-03-04
  • 2018-12-31
  • 2019-11-28
  • 2016-08-18
  • 2020-12-04
  • 1970-01-01
相关资源
最近更新 更多