YAML read filenames stored across multiple variables

46 Views Asked by At

I have YAML data that contains file paths of hundreds of experiments results over a long time.
The results are stored across different directories.
All the results have a same base directory.
Due to this I wanted to create a common variable for root directory and use the root directory variable across the file paths to avoid repeating it.

My YAML DATA: DEMO.yaml

define: &root '/Users/SAL/Documents/Projects/FORD_CELLS/'  
test1 : *root+'test1/result.csv'
test2 : *root+'test2/result.csv'

I want to read the YAML variables in python like this: READ IN PYTHON:

import yaml
import pandas as pd

exp_info = yaml.safe_load(open('DEMO.yaml'))
exp_info['test2']

When I try to read the test results, I am facing ScannerError

ScannerError: while scanning an alias
expected alphabetic or numeric character, but found '+'

I tried different combinations but none of them worked. Can anyone help me how to tackle this situation.

1

There are 1 best solutions below

0
NULL On

With Python, you can use the string format option. See the code below

import yaml
import pandas as pd

root = 'path/'
exp_info = yaml.safe_load(open('DEMO.yaml'))
exp_info['{root}test2/result.csv']

And any additional characters like '+' need to be inside the quotation '