python的VSCode调试如何在json中将列表传递给参数[已关闭]

xzlaal3s  于 2023-01-10  发布在  Python
关注(0)|答案(1)|浏览(125)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。

昨天关门了。
Improve this question
我最近学习了如何在VSCode中调试。但是我遇到了一个问题,我的python代码将regex作为参数,它本身是一个字符串列表,我如何将它传递到json文件中的arg中?
例如,我的代码应该如下运行

python main.py --regex [r'blk_(|-)[0-9]+',r'(/|)([0-9]+\.){3}[0-9]+(:[0-9]+|)(:|)',r'(?<=[^A-Za-z0-9])(\-?\+?\d+)(?=[^A-Za-z0-9])|[0-9]+$']

如何将此正则表达式列表添加到“args”中:的“launch.json”进行调试?以下操作不起作用?

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true,
            "args": ["--regex", "[r'blk_(|-)[0-9]+',r'(/|)([0-9]+\.){3}[0-9]+(:[0-9]+|)(:|)', r'(?<=[^A-Za-z0-9])(\-?\+?\d+)(?=[^A-Za-z0-9])|[0-9]+$']"]
        }
    ]
}

这不会像它应该的那样解析正则表达式列表,但是VSCode在调试时会将其解析为单个字符。

q8l4jmvw

q8l4jmvw1#

您可能需要转义每个字符,因为有很多特殊字符:转义字符的结果为:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true,
            "args": ["--regex", "\[\r\'\b\l\k\_\(\|\-\)\[\0\-\9\]\+\'\,\r\'\(\/\|\)\(\[\0\-\9\]\+\\\.\)\{\3\}\[\0\-\9\]\+\(\:\[\0\-\9\]\+\|\)\(\:\|\)\'\,\ \r\'\(\?\<\=\[\^\A\-\Z\a\-\z\0\-\9\]\)\(\\\-\?\\\+\?\\\d\+\)\(\?\=\[\^\A\-\Z\a\-\z\0\-\9\]\)\|\[\0\-\9\]\+\$\'\]"]
        }
    ]
}

下面是我用来生成它的代码:

i = r"[r'blk_(|-)[0-9]+',r'(/|)([0-9]+\.){3}[0-9]+(:[0-9]+|)(:|)', r'(?<=[^A-Za-z0-9])(\-?\+?\d+)(?=[^A-Za-z0-9])|[0-9]+$']"
print("".join(["\\" + c for c in i]))

相关问题