简体   繁体   English

如何在Shell命令行上传递带引号的多行字符串?

[英]How can I pass a multi-line string with quotes on a shell command line?

I have two scripts and want to call the tester script from the terminal. 我有两个脚本,想从终端调用测试器脚本。

string s = """ output/directory/366d595b-23b2-435d-8dc6-698b3d0844b9/result.csv scores=[{
   "base_score": 0.92,
   "metric": "ACCURACY"
    }]"""

Script #1 (script1.py)- consists of a function that parses a string 脚本1(script1.py)-由解析字符串的函数组成

    def parser_score(s):
         dict_txt = re.search('\[([^]]+)', s).group(1).strip()
         data = json.loads(dict_txt)
         return data

Script #2(script2.py) - calls script #1 and saves the result in a json file 脚本2(script2.py)-调用脚本1并将结果保存在json文件中

    import sys
    from parser_for_score_v3 import parser_score

    s = sys.argv[1]
    print(s)

    result = parser_score(s)

    # save dict to a json file
    with open('result.json', 'w') as fp:
         json.dump(result, fp)

I call the second script from the terminal - 我从终端调用第二个脚本-

    abc $ python script2.py """output/directory/366d595b-23b2-435d-8dc6-698b3d0844b9/result.csv scores=[{
   "base_score": 0.92,
    "metric": "ACCURACY"
    }]"""

I get an error: AttributeError: 'NoneType' object has no attribute 'group' -bash: base_score:: command not found 我收到一个错误:AttributeError:'NoneType'对象没有属性'group'-bash:base_score ::命令未找到

  1. How can I pass the string ( such a long multi-line string) to a python script from the terminal? 如何从终端将字符串(这么长的多行字符串)传递给python脚本? Text file is not an option. 文本文件不是选项。
  2. How can I modify the regex to read a string with or without quotes? 如何修改正则表达式以读取带引号或不带引号的字符串?

Thanks in advance. 提前致谢。

The precise literal multi-line string 精确的文字多行字符串

output/directory/366d595b-23b2-435d-8dc6-698b3d0844b9/result.csv scores=[{
  "base_score": 0.92,
  "metric": "ACCURACY"
}]

...requires nothing more than single quotes to pass it on a shell command line. ...只需要单引号将其传递到Shell命令行即可。 Thus: 从而:

python script2.py 'output/directory/366d595b-23b2-435d-8dc6-698b3d0844b9/result.csv scores=[{
   "base_score": 0.92,
   "metric": "ACCURACY"
}]
'

...will pass that string in sys.argv[1] . ...将在sys.argv[1]传递该字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM