in Education by
I need to use environment variable "PATH" in yaml file which needs to be parsed with a script. This is the environment variable I have set on my terminal: $ echo $PATH /Users/abc/Downloads/tbwork This is my sample.yml: --- Top: ${PATH}/my.txt Vars: - a - b When I parse this yaml file with my script, I don't see PATH variables actual value. This is my script: import yaml import os import sys stream = open("sample.yml", "r") docs = yaml.load_all(stream) for doc in docs: for k,v in doc.items(): print k, "->", v print "\n", Output: Top -> ${PATH}/my.txt Vars -> ['a', 'b'] Expected output is: Top -> /Users/abc/Downloads/tbwork/my.txt Vars -> ['a', 'b'] Can someone help me figuring out the correct way to do it if I am doing it wrong way? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
PY-yaml library doesn't resolve environment variables by default. You need to define an implicit resolver that will find the regex that defines an environment variable and execute a function to resolve it. You can do it through yaml.add_implicit_resolver and yaml.add_constructor. In the code below, you are defining a resolver that will match on ${ env variable } in the YAML value and calling the function path_constructor to look up the environment variable. import yaml import re import os path_matcher = re.compile(r'\$\{([^}^{]+)\}') def path_constructor(loader, node): ''' Extract the matched value, expand env variable, and replace the match ''' value = node.value match = path_matcher.match(value) env_var = match.group()[2:-1] return os.environ.get(env_var) + value[match.end():] yaml.add_implicit_resolver('!path', path_matcher) yaml.add_constructor('!path', path_constructor) data = """ env: ${VAR}/file.txt other: file.txt """ if __name__ == '__main__': p = yaml.load(data, Loader=yaml.FullLoader) print(os.environ.get('VAR')) ## /home/abc print(p['env']) ## /home/abc/file.txt Warning: Do not run this if you are not the one specifying the env variables (or any other untrusted input) as there are remote code execution vulnerabilities with FullLoader as of July 2020.

Related questions

0 votes
    Our core application changed from Python 2.6 to Python 2.7 maybe to Python 3 in later release. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I am using Pycharm 2018.2.4 community edition on Ubuntu 18.04 and I have started python 2.7 ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I am using Pycharm 2018.2.4 community edition on Ubuntu 18.04 and I have started python 2.7 ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    What I'm trying to do here is get the headers of a given URL so I can determine the MIME type. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I'm using MySqldb with Python 2.7 to allow Python to make connections to another MySQL server import ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    Multiple objects can be de parsed at once using the ______ function. (a) dput() (b) write() (c) ... and Operations of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    Multiple objects can be de parsed at once and read back using function _____ (a) source() (b) read() ( ... Operations of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    import os import sys import fileinput print ("Text to search for:") textToSearch = input( "> " ) ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    I have a csv file and I am reading this file in python using pandas. I want to read each row of ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    How to use YAML files in high programming languages such as JAVA, Python, etc?...
asked Jul 28, 2021 in Technology by JackTerrance
0 votes
    object looks like as below { "id":1, "image":"path/to/image", "employee_data":, } again ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    object looks like as below { "id":1, "image":"path/to/image", "employee_data":, } again ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I am looking for a way to return the interpolated coordinate value of a variable at a query variable value ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    What is a YAML file and how do we use it in Ansible?...
asked Jul 28, 2021 in Technology by JackTerrance
...