本文介绍了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.

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 删除参数字段中的引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 07:44