2

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 " ".

Here is the test code:

#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)

This is an example of the running code

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}]"""

and finally the error

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}]"

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!

Khaine775
  • 2,715
  • 8
  • 22
  • 51

1 Answers1

3

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.

Michael Ruth
  • 2,938
  • 1
  • 20
  • 27
  • with one single quote (') it stops at the first 2020 with single double quote (") it stops at 0237 next to "ingeteam ", with double double quotes ("") it stops again in the first 2020, and with triple double quotes (""") again in 0237. Not sure how it works :/ – Cristobal Rencoret Apr 07 '20 at 12:59
  • @CristobalRencoret: Is there a single quote in the text somewhere? – rici Apr 07 '20 at 13:13
  • @rici there is none, only double Q ( " ) – Cristobal Rencoret Apr 07 '20 at 13:35
  • @CristobalRencoret: OK. I tried it with a single quote and it worked perfectly. It fails with a double single quote, but you claim you used one single quote and as far as I can see, that will work as expected. – rici Apr 07 '20 at 14:32
  • i am using windows 10 anaconda prompt to run it what are you using? @rici – Cristobal Rencoret Apr 07 '20 at 14:36
  • 1
    @cristobal: Bash on Linux. So you're probably running into some wierdness with Windows. It's always good to add details like that to your question. – rici Apr 07 '20 at 14:40
  • 1
    [Single quotes apparently don't work in the Windows shell](https://stackoverflow.com/a/24181667/1566221). Also, if you're using PowerShell, the `run-native` function from [this answer](https://stackoverflow.com/a/21334121/1566221) might be useful. – rici Apr 07 '20 at 14:50