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.

Rishabh Rusia
  • 173
  • 2
  • 4
  • 19
  • http://jsonlint.com/ – Paul Collingwood Mar 06 '17 at 08:34
  • @PaulCollingwood , its not about validating JSON, its about taking input from command line when ' punctuation id detected earlier than the actual input end – Rishabh Rusia Mar 06 '17 at 08: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. http://stackoverflow.com/questions/15637429/how-to-escape-double-quotes-in-json – Paul Collingwood Mar 06 '17 at 19:35

1 Answers1

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}]'
sjjhsjjh
  • 333
  • 1
  • 7
  • 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 – Rishabh Rusia Mar 06 '17 at 09:45
  • Maybe you could edit your question to include your sentence score Python script? – sjjhsjjh Mar 07 '17 at 21:17
  • 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 – Rishabh Rusia Mar 08 '17 at 06:38
  • 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. – sjjhsjjh Mar 11 '17 at 09:24