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!

3
  • 2
    Look at sys.argv[1:]. That's what argparse looks at. Your JSON has appear as one long string in that list. Just how much quoting that requires will depend on your OS and shell. Commented Apr 7, 2020 at 2:44
  • what is the command to simulate the CMD content of an argument (-contenido xxxxxxxxx) so that the sys returns xxxxxxxxx? Commented Apr 7, 2020 at 13:05
  • It was a CMD / powershell problem in ubuntu it runs. TY all Commented Apr 7, 2020 at 20:14

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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 :/
@CristobalRencoret: Is there a single quote in the text somewhere?
@rici there is none, only double Q ( " )
@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.
Single quotes apparently don't work in the Windows shell. Also, if you're using PowerShell, the run-native function from this answer might be useful.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.