PyCharm deletes quotation marks in paramenter field
Asked Answered
P

1

6

I want to set a parameter for a python script by using the parameter field in PyCharm.

My config:

run configuration

But the command in the Run console is:

python3 path_to_script.py '{app_id: picoballoon_network, dev_id: ferdinand_8c ... and so on

and not:

python3 path_to_script.py '{"app_id": "picoballoon_network", "dev_id": "ferdinand_8c" ... and so on

Basically, it deletes all " in the parameter. Does anyone know how to turn this off?

My PyCharm version is:

PyCharm 2020.3.1 (Professional Edition)
Build #PY-203.6682.86, built on January 4, 2021
Runtime version: 11.0.9.1+11-b1145.37 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 10 10.0
Passant answered 4/1, 2021 at 19:40 Comment(0)
K
4

To avoid the quotation marks being deleted notice the rules to writing parameters that contain quotation marks.

Run/Debug Configuration: Python

Configuration tab

When specifying the script parameters, follow these rules:

  • Use spaces to separate individual script parameters.

  • Script parameters containing spaces should be delimited with double quotes, for example, some" "param or "some param".

  • If script parameter includes double quotes, escape the double quotes with backslashes, for example:

-s"main.snap_source_dirs=[\"pcomponents/src/main/python\"]" -s"http.cc_port=8189"
-s"backdoor.port=9189"
-s"main.metadata={\"location\": \"B\", \"language\": \"python\", \"platform\": \"unix\"}"

The case in the question would be a single parameter, lets apply the rules to the example:

'{"app_id": "picoballoon_network", "dev_id": "ferdinand_8c"'

  1. Because it's a single parameter containing spaces it has to be surounded by quotation marks.

  2. Since the content of the parameter also contains quotation marks they must be escaped using a backslash \. So applying the parameter formatting rules gives:

"'{\"app_id\": \"picoballoon_network\", \"dev_id\": \"ferdinand_8c\"}'"

  1. (Side note): In the example the parameter was surrounded by Apostrophes, this may be unnecessary and will probably have to be stripped later in your Python code (the below example uses the strip method).

You can test it with this simple script:

import sys
import ast


your_dictionary = ast.literal_eval(sys.argv[1].strip("'"))

(Side note): Your example parameter is a string containing a Python dictionary, there are several ways to convert it, in the example I included the highest voted answer from this question: "Convert a String representation of a Dictionary to a dictionary?"

A screenshot showing the parameter and test code in use:

enter image description here

Kitty answered 5/1, 2021 at 1:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.