Pycharm 删除参数字段中的引号 [英] Pycharm deletes quotation marks in paramenter field

查看:165
本文介绍了Pycharm 删除参数字段中的引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用 PyCharm 中的参数字段为 python 脚本设置参数.

我的配置:

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

python3 path_to_script.py '{app_id: picoballoon_network, dev_id: ferdinand_8c ... 等等

而不是:

python3 path_to_script.py '{"app_id": "picoballoon_network", "dev_id": "ferdinand_8c";...等等

基本上,它会删除参数中的所有".有谁知道如何关闭它?

我的 PyCharm 版本是:

PyCharm 2020.3.1(专业版)Build #PY-203.6682.86,建于 2021 年 1 月 4 日运行时版本:11.0.9.1+11-b1145.37 amd64VM:JetBrains s.r.o. 的 OpenJDK 64 位服务器 VM.视窗 10 10.0

解决方案

为了避免引号被删除,请注意写包含引号的参数的规则.

<块引用>

运行/调试配置:Python

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

My config:

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

解决方案

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:

这篇关于Pycharm 删除参数字段中的引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆