0

There are some questions posted on the same topic regarding this but my concern is something else. I am trying to take dictionary input from command line and able to do it.

python sentence_scorev1.3.py "working today" "0.6" '[{"ques": "hello who are you", "ans": "I am rishabh", "type": 1},{"ques": "your name", "ans": "I am Ram", "type": 2},{"ques": "Are you working today", "ans": "Yes I am", "type": 4'}]'

I have taken the input and converted it into JSON and parse it using

json_data = json.loads(input_data)

where input data is the hash input. My concern is when I pass the same input with ' punctuation in the hash input like see your' in second ques key input

'[{"ques": "hello who are you", "ans": "I am rishabh", "type": 1},{"ques": "your' name", "ans": "I am Ram", "type": 2}

It throws error as the python must have understood it as end of input but still I had input data string. Please let me know how to by-pass this.

3
  • jsonlint.com Commented Mar 6, 2017 at 8:34
  • @PaulCollingwood , its not about validating JSON, its about taking input from command line when ' punctuation id detected earlier than the actual input end Commented Mar 6, 2017 at 8:48
  • The point is not to expect your users to enter well formed JSON strings. You can enter such strings on the command line if you use escape characters. stackoverflow.com/questions/15637429/… Commented Mar 6, 2017 at 19:35

1 Answer 1

1

If you're launching Python from a Unix shell or similar ...

The punctuation will be interpreted as the end of the third command line argument, which starts with the ' character to the left of the first square bracket. To stop the shell from doing that, escape the ' with a backslash like this:

'[{"ques": "hello who are you", "ans": "I am rishabh", "type": 1},{"ques": "your\' name", "ans": "I am Ram", "type": 2}]'

(I tried to add balancing parentheses at the end.)

The parsing happens before Python even gets the command line arguments.

Edit: The original command line includes an extra single quote. I think it should be like this:

'[{"ques": "hello who are you", "ans": "I am rishabh", "type": 1},{"ques": "your name", "ans": "I am Ram", "type": 2},{"ques": "Are you working today", "ans": "Yes I am", "type": 4}]'
Sign up to request clarification or add additional context in comments.

4 Comments

I am using ubuntu terminal to execute and if I pass it as escape, it enters command line execution as : > > and the script doesn't executes
Maybe you could edit your question to include your sentence score Python script?
For now I have found a way. I will put # instead of ' in command line to take input. And in the script I have replaced # by '. For time being it can be used but not a correct approach. Looking for some genuine trick here
Another approach could be to create your JSON in a programmers' text editor that has syntax checking, then save it as a file, then read the file instead.

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.