简体   繁体   中英

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

    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

    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

  1. How can I pass the string ( such a long multi-line string) to a python script from the terminal? 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. 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] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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