Pycharm删除参数字段中的引号

2024-07-02 14:12:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用PyCharm中的参数字段为python脚本设置一个参数

我的配置:

enter image description here

但运行控制台中的命令是:

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

而不是:

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

基本上,它会删除参数中的所有"。 有人知道怎么关掉这个吗

我的PyCharm版本是:

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

Tags: andtopathpydevidapp参数
1条回答
网友
1楼 · 发布于 2024-07-02 14:12:09

要避免删除引号,请注意写入包含引号的参数的规则

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

问题中的情况是单个参数,让我们将规则应用于示例:

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

  1. 因为它是一个包含空格的单个参数,所以必须用引号括起来

  2. 由于参数的内容还包含引号,因此必须使用反斜杠\对其进行转义。因此,应用参数格式规则可以得到:

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

  1. (旁注):在示例中,参数被Apostrophes包围,这可能是不必要的,并且以后可能需要在Python代码中进行剥离(下面的示例使用^{}方法)

您可以使用以下简单脚本进行测试:

import sys
import ast


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

(旁注):您的示例参数是一个包含Python dictionary的字符串,有几种方法可以转换它,在示例中,我包含了这个问题中投票率最高的答案:"Convert a String representation of a Dictionary to a dictionary?"

显示正在使用的参数和测试代码的屏幕截图:

enter image description here

相关问题 更多 >